1#ifndef TMVA_SOFIE_ROPERATOR_SLICE
2#define TMVA_SOFIE_ROPERATOR_SLICE
13namespace Experimental{
18template <
typename IType>
57 for (
size_t i = 0; i < names.size(); ++i) {
78 throw std::runtime_error(
"TMVA Slice Op Input Tensor is not found in model");
81 std::vector<std::vector<Dim>> shapes;
85 std::vector<std::vector<IType>>
itensors(4);
89 for (
size_t i = 0; i < 4; ++i) {
111 for (
size_t k = 0; k < s; k++) {
146 for (
size_t i = 0; i <
fAxes.size(); i++) {
150 throw std::runtime_error(
"TMVA Slice Op : invalid axis value " + std::to_string(
fAxes[i]) +
151 " for " + std::to_string(i));
155 for (
size_t i = 0; i <
fAxes.size(); i++) {
159 throw std::runtime_error(
"TMVA Slice Op : Missing start input tensor");
164 throw std::runtime_error(
"TMVA Slice Op : Missing end input tensor");
190 throw std::runtime_error(
"TMVA Slice Op : parametric step inputs are not supported");
200 }
else if (
istep < 0) {
205 throw std::runtime_error(
"TMVA Slice Op : invalid step value " + std::to_string(
istep) +
206 " for " + std::to_string(i));
236 std::string send = std::string(
"(") +
fShapeInput[
fAxes[i]].param +
"-" + std::to_string(-
iend) +
")";
250 fShapeOutput.resize(dim);
251 for (
size_t i = 0; i < dim; i++) {
252 if (!fEnd[i].isParam && !fStart[i].isParam && !fSteps[i].isParam) {
253 int64_t
istart =
static_cast<int64_t
>(fStart[i].dim);
254 int64_t
iend =
static_cast<int64_t
>(fEnd[i].dim);
255 int64_t
istep=
static_cast<int64_t
>(fSteps[i].dim);
257 fShapeOutput[i] = Dim{
static_cast<size_t>(s)};
260 if (fStart[i].GetVal() !=
"0")
261 s =
"(" + fEnd[i].GetVal() +
"-" + fStart[i].GetVal() +
")";
263 s = fEnd[i].GetVal();
264 if (fSteps[i].GetVal() !=
"1") {
266 s +=
")/" + fSteps[i].GetVal() +
")";
268 fShapeOutput[i] = Dim{s,size_t(-1)};
271 if (fEnd[i].isParam && fEnd[i].dim !=
size_t(-1))
272 model.AddShapeParam(fEnd[i].param,fEnd[i].dim );
273 if (fStart[i].isParam && fStart[i].dim !=
size_t(-1))
274 model.AddShapeParam(fStart[i].param,fStart[i].dim );
275 if (fSteps[i].isParam && fSteps[i].dim !=
size_t(-1))
276 model.AddShapeParam(fSteps[i].param,fSteps[i].dim );
281 if (model.IsInitializedTensor(fNData) && model.GetTensorType(fNData) ==
ETensorType::INT64) {
282 fIsOutputConstant =
true;
283 auto inputData =
static_cast<int64_t*
>(model.GetInitializedTensorData(fNData).get());
288 std::cout <<
" start - end -steps \n";
289 for (
size_t ii = 0;
ii< fStart.size();
ii++)
290 std::cout << fStart[
ii] <<
" " << fEnd[
ii] <<
" " << fSteps[
ii] << std::endl;
294 std::cout <<
"SLice_impl " << fStart.size() <<
" " << fEnd.size() <<
" " << fSteps.size() <<
" " <<
iax << std::endl;
295 if (fStart[
iax].isParam || fEnd[
iax].isParam || fSteps[
iax].isParam)
296 throw std::runtime_error(
"TMVA Slice Op : cannot have parametric values when input is constant");
298 std::vector<IType> indices;
300 indices.push_back(i);
302 std::cout <<
"SLice_impl last axis: " << indices.size() <<
" : ";
303 for (
size_t i = 0; i < indices.size(); i++) {
308 std::cout << std::endl;
311 std::cout <<
"SLice_impl else : " << indices.size() <<
" : ";
312 for (
size_t i = 0; i < indices.size(); i++) {
317 std::cout << std::endl;
327 if (model.Verbose()) {
328 std::cout <<
"Slice: output is a constant tensor " <<
ConvertShapeToString(fShapeOutput) <<
" : "
333 model.AddIntermediateTensor(fNOutput, model.GetTensorType(fNData), fShapeOutput);
334 if (model.Verbose()) {
335 std::cout <<
"Slice ---> " << fNOutput <<
" " <<
ConvertShapeToString(fShapeOutput) << std::endl;
341 if (fIsOutputConstant)
return "";
344 if (fShapeInput.empty() || fShapeOutput.empty()){
345 throw std::runtime_error(
"TMVA SOFIE Slice Op called to Generate without being initialized first");
348 std::stringstream out;
351 out << SP <<
"///------- Slice operator\n" << std::endl;
353 size_t ndim = fShapeInput.size();
354 auto strides = UTILITY::ComputeStrideFromShape(fShapeInput);
358 for (
size_t i = 0; i < fStepDims.size(); i++) {
359 if (fStepDims[i].isParam) {
361 out << SP <<
"size_t " << fStepDims[i] <<
" = tensor_" << fNames[3] <<
"[" << i <<
"];\n";
365 for (
size_t i = 0; i < fStartDims.size(); i++) {
366 if (fStartDims[i].isParam && fStartDims[i].param != fShapeInput[fAxes[i]].param) {
367 std::string
s_start =
"start_" + std::to_string(i);
370 out << SP <<
"size_t " <<
s_start <<
" = tensor_" << fNames[0] <<
"[" << i <<
"];\n";
372 out << SP <<
"size_t " <<
s_start <<
" = " << fStartDims[i] <<
";\n";
375 out << SP <<
"if (" <<
s_start <<
" < 0) " <<
s_start <<
" += " << fShapeInput[fAxes[i]] <<
";\n";
376 out << SP <<
"if (" <<
s_start <<
" < 0) " <<
s_start <<
" = 0;\n";
377 if (!fStepDims[i].isParam) {
378 if (
static_cast<IType>(fStepDims[i].dim) > 0 )
379 out << SP <<
"if (" <<
s_start <<
" > " << fShapeInput[fAxes[i]] <<
" ) " <<
s_start <<
" = " << fShapeInput[fAxes[i]] <<
";\n";
381 out << SP <<
"if (" <<
s_start <<
" > " << fShapeInput[fAxes[i]] <<
" - 1" <<
" ) " <<
s_start <<
" = " << fShapeInput[fAxes[i]] <<
" - 1;\n";
385 else if (fStartDims[i].isParam && fStartDims[i].param == fShapeInput[fAxes[i]].param && !fStepDims[i].isParam &&
static_cast<IType>(fStepDims[i].dim) < 0 ) {
386 fStart[fAxes[i]] =
Dim{ fStartDims[i].param +
"-1" };
390 for (
size_t i = 0; i < fEndDims.size(); i++) {
391 if (fEndDims[i].isParam && fEndDims[i].param != fShapeInput[fAxes[i]].param) {
392 std::string
s_end =
"end_" + std::to_string(i);
394 s_end = fEndDims[i].param;
395 out << SP <<
"size_t " <<
s_end <<
" = tensor_" << fNames[1] <<
"[" << i <<
"];\n";
397 out << SP <<
"size_t " <<
s_end <<
" = " << fEndDims[i] <<
";\n";
398 fEnd[fAxes[i]] =
s_end;
400 out << SP <<
"if (" <<
s_end <<
" < 0) " <<
s_end <<
" += " << fShapeInput[fAxes[i]] <<
";\n";
401 if (!fStepDims[i].isParam) {
402 if (
static_cast<IType>(fStepDims[i].dim) > 0 ) {
403 out << SP <<
"if (" <<
s_end <<
" < 0) " <<
s_end <<
" = 0;\n";
404 out << SP <<
"if (" <<
s_end <<
" > " << fShapeInput[fAxes[i]] <<
" ) " <<
s_end <<
" = " << fShapeInput[fAxes[i]] <<
";\n";
406 out << SP <<
"if (" <<
s_end <<
" < -1) " <<
s_end <<
" = -1;\n";
407 out << SP <<
"if (" <<
s_end <<
" > " << fShapeInput[fAxes[i]] <<
" - 1" <<
" ) " <<
s_end <<
" = " << fShapeInput[fAxes[i]] <<
" - 1;\n";
412 else if (fEndDims[i].isParam && fEndDims[i].param == fShapeInput[fAxes[i]].param && !fStepDims[i].isParam &&
static_cast<IType>(fStepDims[i].dim) < 0 ) {
413 fEnd[fAxes[i]] =
Dim{ fEndDims[i].param +
"-1" };
417 out << SP <<
"size_t iOut = 0;\n";
418 std::string
MSP = SP;
420 out <<
MSP <<
"for (size_t i" <<
idim <<
" = " << fStart[
idim] <<
"; i" <<
idim <<
" < " << fEnd[
idim]
421 <<
"; i" <<
idim <<
"+= " << fSteps[
idim] <<
") {\n";
423 if (
idim < ndim-1) out <<
MSP <<
"size_t stride" <<
idim <<
" = " << strides[
idim] <<
"*i" <<
idim <<
";\n";
425 out <<
MSP <<
"size_t iInput = ";
426 for (
size_t idim = 0;
idim < ndim-1;
idim++) out <<
" stride" <<
idim <<
" + ";
428 out <<
"i" << ndim-1 <<
";\n";
429 out <<
MSP <<
"tensor_" << fNOutput <<
"[iOut++] = tensor_" <<fNData <<
"[iInput];\n";
431 MSP =
MSP.replace(0,SP.length(),
"");
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
std::vector< size_t > GetTensorShape(const std::string &name) const
std::vector< Dim > GetDimTensorShape(const std::string &name) const
bool CheckIfTensorAlreadyExist(std::string tensor_name)
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
bool IsInitializedTensor(const std::string &name) const
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
std::vector< std::vector< IType > > fAttributes
std::vector< Dim > fEndDims
std::vector< Dim > fStart
std::vector< Dim > fShapeOutput
std::vector< Dim > fSteps
std::vector< Dim > fStartDims
std::vector< Dim > fShapeInput
std::vector< IType > fAxes
void Initialize(RModel &model) override
std::vector< Dim > fStepDims
ROperator_Slice(std::string nameData, std::vector< IType > starts, std::vector< IType > ends, std::vector< IType > axes, std::string nameOutput)
std::string Generate(std::string OpName) override
ROperator_Slice(std::string nameData, std::vector< std::string > names, std::string nameOutput)
std::vector< std::string > fNames
std::vector< std::string_view > fInputTensorNames
std::vector< std::string_view > fOutputTensorNames
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations