From cd89d7f0dabf91bab547d354f0ef68cc1dadf0a4 Mon Sep 17 00:00:00 2001 From: Kalle Raiskila Date: Sat, 29 Jul 2023 12:41:34 +0200 Subject: [PATCH] Remove local tensor copies resize, upsample, scatternd, shape, slice nodes --- src/nodes/resize.h | 93 +++++++++++++++++++----------------------- src/nodes/scatternd.cc | 10 +++-- src/nodes/scatternd.h | 6 --- src/nodes/shape.h | 22 +++------- src/nodes/slice.h | 19 ++++----- src/nodes/upsample.h | 14 +++---- 6 files changed, 66 insertions(+), 98 deletions(-) diff --git a/src/nodes/resize.h b/src/nodes/resize.h index 8e09f37..df0ddd3 100644 --- a/src/nodes/resize.h +++ b/src/nodes/resize.h @@ -11,7 +11,6 @@ class Resize : public Node { public: Resize() { op_name = "Resize"; - X=roi=scales=sizes=Y=NULL; coordinate_transformation_mode = "half_pixel"; cubic_coeff_a = -0.75; exclude_outside = 0; @@ -27,15 +26,6 @@ class Resize : public Node { std::string mode; std::string nearest_mode; - // inputs - const Tensor *X; - // optional inputs - const Tensor *roi; - const Tensor *scales; - const Tensor *sizes; - // outputs - const Tensor *Y; - std::vectordim_scales; // 'scales' value when calculating coordinate transforms /* Parse attributes, if this node has them. */ @@ -59,18 +49,44 @@ class Resize : public Node { } } + const Tensor *get_roi(void) const + { + if(inputs.size() > 1 && inputs[1]->is_used() ) + return inputs[1]; + else + return nullptr; + } + const Tensor *get_scales(void) const + { + if(inputs.size() > 2 && inputs[2]->is_used() ) + return inputs[2]; + else + return nullptr; + } + const Tensor *get_sizes(void) const + { + if(inputs.size() > 3) + return inputs[3]; + else + return nullptr; + } + /* Assign input tensors, resolve output tensor shapes, allocate output tensors */ virtual void resolve(void) override { - X = inputs[0]; - - if (inputs.size() == 2) - roi = inputs[1]; - if (inputs.size() == 3 && inputs[2]->name != "") - scales = inputs[2]; - if (inputs.size() == 4) - sizes = inputs[3]; + const Tensor *X = inputs[0]; + const Tensor *roi = get_roi(); + const Tensor *scales = get_scales(); + const Tensor *sizes = get_sizes(); + + register_input(X, "X"); + if (roi) + register_input(roi, "roi"); + if (scales) + register_input(scales, "scales"); + if (sizes) + register_input(sizes, "sizes"); // "One of 'scales' and 'sizes' MUST be specified and it is an error if both are specified." if (scales == NULL && sizes == NULL) @@ -102,46 +118,19 @@ class Resize : public Node { /* Create output tensors. * Set data dimensions and data type for the created tensors. */ Tensor *t = new Tensor; - - for( auto s : output_size ) t->data_dim.push_back(s); t->data_type = onnx::TensorProto_DataType_FLOAT; - /* Store the created tensor both as reference in this node, and into - * the return value vector! */ - Y = t; - outputs.push_back(t); + register_output(t, "Y"); - /* TODO: optional outputs? */ } - /* Print the function parameters - use the order they are introduced in the - * ONNX documentation */ - virtual void print_parameters(std::ostream &dst, bool decorate ) const override - { - X->print_tensor_as_const(dst, !decorate); - - if (roi) { - dst << ", "; - roi->print_tensor_as_const(dst, !decorate); - } - if (scales) { - dst << ", "; - scales->print_tensor_as_const(dst, !decorate); - } - if (sizes) { - dst << ", "; - sizes->print_tensor_as_const(dst, !decorate); - } - - dst << ", "; - Y->print_tensor(dst, !decorate); - } - /* Print the coordinate transform algorithm, without integer roundings. */ std::string coordinate_transformation( int dim, std::string y_coordinate) const { + const Tensor *X = inputs[0]; + const Tensor *Y = outputs[0]; std::string scale = std::to_string(dim_scales[dim]); std::string x_dimsize = std::to_string(X->data_dim[dim]); std::string y_dimsize = std::to_string(Y->data_dim[dim]); @@ -187,6 +176,7 @@ class Resize : public Node { /* For the mode 'nearest', calculate the rounding of x_resized to indexes */ std::string x_coord_nearest( int dim) const { + const Tensor *X = inputs[0]; std::string x_dimsize = std::to_string(X->data_dim[dim]); std::string x_resized = "x_orig_" + std::to_string(dim); // Apply rounding @@ -219,6 +209,7 @@ class Resize : public Node { void print_calc_nearest(std::ostream &dst) const { + const Tensor *Y = outputs[0]; std::string out = "Y"; std::string in = "X"; unsigned n_data_dims = Y->rank(); @@ -232,6 +223,8 @@ class Resize : public Node { void print_calc_linear(std::ostream &dst) const { + const Tensor *X = inputs[0]; + const Tensor *Y = outputs[0]; std::string out = "Y"; std::string in = "X"; std::vector interpolate_dims; @@ -351,11 +344,9 @@ class Resize : public Node { INDT_1 << " * " << s << std::endl; INDT_1 << " */" << std::endl; + const Tensor *Y = outputs[0]; unsigned n_data_dims = Y->rank(); - INDT_1 << cast_to_ndim_arrayptr(X, "X") << std::endl; - INDT_1 << cast_to_ndim_arrayptr(Y, "Y") << std::endl; - // loop over output for( unsigned i = 0; idata_dim = data->data_dim; t->data_type = data->data_type; - output = t; register_output(t, "output"); } void ScatterND::print(std::ostream &dst) const { + const Tensor *data = inputs[0]; + const Tensor *indices = inputs[1]; + const Tensor *output = outputs[0]; unsigned k = indices->data_dim[indices->rank()-1]; std::string data_op="="; diff --git a/src/nodes/scatternd.h b/src/nodes/scatternd.h index 9fe2e96..5a1551c 100644 --- a/src/nodes/scatternd.h +++ b/src/nodes/scatternd.h @@ -10,16 +10,10 @@ class ScatterND : public Node { public: ScatterND() { op_name = "ScatterND"; - data=indices=updates=output=NULL; reduction=""; } std::string reduction; - const Tensor *data; - const Tensor *indices; - const Tensor *updates; - const Tensor *output; - virtual void parseAttributes( onnx::NodeProto &node ) override; virtual void resolve(void) override; virtual void print(std::ostream &dst) const override; diff --git a/src/nodes/shape.h b/src/nodes/shape.h index 0214b14..a76cb2b 100644 --- a/src/nodes/shape.h +++ b/src/nodes/shape.h @@ -8,16 +8,13 @@ class Shape : public Node { public: Shape() { op_name = "Shape"; - data=output=NULL; } - const Tensor *data; - const Tensor *output; - virtual void resolve(void) override { - data = inputs[0]; + const Tensor *data = inputs[0]; + register_input(data, "data"); Tensor *t = new Tensor; t->data_dim.push_back(data->rank()); @@ -30,21 +27,14 @@ class Shape : public Node { db[i] = data->data_dim[i]; t->data_buffer = (void*) db; - output = t; - outputs.push_back(t); - } - - - virtual void print_parameters(std::ostream &dst, bool decorate ) const override - { - data->print_tensor_as_const(dst, !decorate); - dst << ", "; - output->print_tensor(dst, !decorate); + register_output(t, "output"); } virtual void print(std::ostream &dst) const override { + const Tensor *data = inputs[0]; + const Tensor *output= outputs[0]; INDT_1 << "/* Shape */" << std::endl; @@ -55,7 +45,7 @@ class Shape : public Node { return; for( unsigned d = 0; drank(); d++ ) { - INDT_1 << output->cname() << "["<sta; @@ -51,7 +41,11 @@ class Slice : public Node { virtual void resolve(void) override { - data = inputs[0]; + const Tensor *data = inputs[0]; + const Tensor *starts = nullptr; + const Tensor *ends= nullptr; + const Tensor *axes= nullptr; + const Tensor *steps= nullptr; register_input(data, "data"); if (inputs.size() > 1) { @@ -203,13 +197,14 @@ class Slice : public Node { stp=stp_; t->data_type = data->data_type; - output = t; register_output(t, "output"); } /* Body of the node implementing function */ virtual void print(std::ostream &dst) const override { + const Tensor *data= inputs[0]; + const Tensor *output = outputs[0]; INDT_1 << "/* Slice */" << std::endl; std::string out_idx, in_idx; diff --git a/src/nodes/upsample.h b/src/nodes/upsample.h index 9476c70..63730c0 100644 --- a/src/nodes/upsample.h +++ b/src/nodes/upsample.h @@ -15,9 +15,10 @@ class Upsample : public Resize { virtual void resolve(void) override { - X = inputs[0]; - - scales = inputs[1]; + const Tensor *X = inputs[0]; + const Tensor *scales = inputs[1]; + register_input(X, "X"); + register_input(scales, "scales"); if( scales->isConst == false ) ERROR("Unimplemented: Upsample 'sizes' input is not a compile-time constant: " + scales->name); @@ -34,18 +35,13 @@ class Upsample : public Resize { /* Create output tensors. * Set data dimensions and data type for the created tensors. */ Tensor *t = new Tensor; - - for( auto s : output_size ) t->data_dim.push_back(s); t->data_type = onnx::TensorProto_DataType_FLOAT; /* Store the created tensor both as reference in this node, and into * the return value vector! */ - Y = t; - outputs.push_back(t); + register_output(t, "Y"); } - - }; }