Skip to content

Commit

Permalink
Remove local tensor copies
Browse files Browse the repository at this point in the history
resize, upsample, scatternd, shape, slice nodes
  • Loading branch information
kraiskil committed Aug 1, 2023
1 parent e5d7abc commit cd89d7f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 98 deletions.
93 changes: 42 additions & 51 deletions src/nodes/resize.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::vector<float>dim_scales; // 'scales' value when calculating coordinate transforms

/* Parse attributes, if this node has them. */
Expand All @@ -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)
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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<int> interpolate_dims;
Expand Down Expand Up @@ -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; i<n_data_dims; i++) {
std::string i_str = std::to_string(i);
Expand Down
10 changes: 6 additions & 4 deletions src/nodes/scatternd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ void ScatterND::resolve(void)
if (inputs.size() != 3) {
ERROR("Wrong number of inputs to ScatterND");
}
data = inputs[0];
indices = inputs[1];
updates = inputs[2];
const Tensor *data = inputs[0];
const Tensor *indices = inputs[1];
const Tensor *updates = inputs[2];
register_input(data, "data");
register_input(indices, "indices");
register_input(updates, "updates");

Tensor *t = new Tensor;
t->data_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="=";
Expand Down
6 changes: 0 additions & 6 deletions src/nodes/scatternd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 6 additions & 16 deletions src/nodes/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;

Expand All @@ -55,7 +45,7 @@ class Shape : public Node {
return;

for( unsigned d = 0; d<data->rank(); d++ ) {
INDT_1 << output->cname() << "["<<d<<"]=";
INDT_1 << "output["<<d<<"]=";
dst << data->data_dim[d] << ";";
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/nodes/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ class Slice : public Node {
public:
Slice() {
op_name = "Slice";
output=data=starts=ends=axes=steps=NULL;
}

// input and output
const Tensor *output;
const Tensor *data;
const Tensor *starts;
const Tensor *ends;
// optional inputs
const Tensor *axes;
const Tensor *steps;

// contents of the input tensors, attributes or default values; padded
// to output dimensions in resolve(void).
std::vector<int64_t>sta;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions src/nodes/upsample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}


};
}

0 comments on commit cd89d7f

Please sign in to comment.