Skip to content

Commit

Permalink
Remove local copies of inputs and outputs
Browse files Browse the repository at this point in the history
dropout, dynamicquantizelinear, elementwise,
elementwise_2, elementwise_variadic, flatten,
gemm, globalaveragepool, lrn, softmax, squeeze,
transpose, unsqueeze
  • Loading branch information
kraiskil committed Aug 1, 2023
1 parent cd89d7f commit b382b82
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 374 deletions.
64 changes: 17 additions & 47 deletions src/nodes/dropout.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,12 @@ class Dropout : public Node {
public:
Dropout() {
op_name = "Dropout";
/* TODO: initialize class variables (attributes and tensors) */
data=ratio=training_mode=output=mask=NULL;
seed_given=false;
}
/* Node attributes */
int seed;
bool seed_given; //not an attribute

/* input */
const Tensor *data;
/* optional inputs */
const Tensor *ratio;
const Tensor *training_mode;
/* output */
const Tensor *output;
/* optional output */
const Tensor *mask;

virtual void parseAttributes( onnx::NodeProto &node ) override {
for( const auto& a : node.attribute() ) {
if( a.name() == "seed" ) {
Expand All @@ -41,80 +29,62 @@ class Dropout : public Node {
}
}

virtual void print_parameters(std::ostream &dst, bool decorate ) const override
{
data->print_tensor_as_const(dst, !decorate);
dst << ", ";

if( ratio ) {
ratio->print_tensor_as_const(dst, !decorate);
dst << ", ";
}
if( training_mode ) {
training_mode->print_tensor_as_const(dst, !decorate);
dst << ", ";
}


output->print_tensor(dst, !decorate);

if( mask ) {
dst << ", ";
mask->print_tensor(dst, !decorate);
}
}

/* Body of the node implementing function */
virtual void print(std::ostream &dst) const override
{
const Tensor *data = inputs[0];
std::string datatype = data->data_type_str();
dst << "\t/* Dropout */" << std::endl;

dst << "\t" << datatype << " *in = (" << datatype << "*)" << data->cname() << ";" << std::endl;
dst << "\t" << datatype << " *out = (" << datatype << "*)" << output->cname() << ";" << std::endl;
if( mask )
dst << "\t" << "bool *mask = (bool*)" << mask->cname() << ";" << std::endl;
dst << "\t" << datatype << " *in = (" << datatype << "*)input;" << std::endl;
dst << "\t" << datatype << " *out = (" << datatype << "*)output;" << std::endl;
if( is_output_N_used(1) )
dst << "\t" << "bool *mask_1d = (bool*)mask;" << std::endl;

dst << "\tfor( uint32_t d=0; d<" << data->data_num_elem() << "; d++) {" << std::endl;

dst << "\t\t" << "out[d] = in[d];" << std::endl;

// NB: specifications say mask can have 'false' values only when training the network.
if( mask )
dst << "\t\t" << "mask[d] = true;" << std::endl;
if( is_output_N_used(1) )
dst << "\t\t" << "mask_1d[d] = true;" << std::endl;
dst << "\t}" << std::endl;
}


virtual void resolve(void) override
{
data = inputs[0];
const Tensor *data = inputs[0];
const Tensor *ratio = nullptr;
const Tensor *training_mode = nullptr;
register_input(data, "input");
if( typeConstraint_highPrecisionNumeric(data) == false )
ERROR("Incorrect input for node");

if( inputs.size() > 1 )
if( inputs.size() > 1 ) {
ratio = inputs[1];
register_input(ratio, "ratio");
}

if( inputs.size() > 2 ) {
ERROR("Unimplemented - training_mode input to Dropout");
training_mode = inputs[2];
register_input(training_mode, "training_mode");
}

/* Create output tensor */
Tensor *rv = new Tensor;
rv->data_dim = data->data_dim;
rv->data_type = data->data_type;
output=rv;
outputs.push_back(rv);
register_output(rv, "output");

/* Mask is optional */
if( is_output_N_used(1) )
{
rv = new Tensor;
rv->data_dim = data->data_dim;
rv->data_type = onnx::TensorProto_DataType_BOOL;
mask=rv;
outputs.push_back(rv);
register_output(rv, "mask");
}
}
};
Expand Down
43 changes: 12 additions & 31 deletions src/nodes/dynamicquantizelinear.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ class DynamicQuantizeLinear : public Node {
public:
DynamicQuantizeLinear() {
op_name = "DynamicQuantizeLinear";
x=y=y_scale=y_zero_point=NULL;
}

const Tensor *x;
const Tensor *y;
const Tensor *y_scale;
const Tensor *y_zero_point;


virtual void print_parameters(std::ostream &dst, bool decorate ) const override
{
x->print_tensor_as_const(dst, !decorate);
dst << ", ";
y->print_tensor(dst, !decorate);
dst << ", ";
y_scale->print_tensor(dst, !decorate);
dst << ", ";
y_zero_point->print_tensor(dst, !decorate);
}

virtual void parseAttributes( onnx::NodeProto &node ) override {
Expand All @@ -40,14 +22,15 @@ class DynamicQuantizeLinear : public Node {

virtual void print(std::ostream &dst) const override
{
const Tensor *x = inputs[0];
int n_el = x->data_num_elem();

INDT_1 << "/* DynamicQuantizeLinear */" << std::endl;

INDT_1 << "float *in_data = (float*)" << x->cname() << ";" << std::endl;
INDT_1 << "uint8_t *out_data = (uint8_t*)" << y->cname()<< ";" << std::endl;
INDT_1 << "float *y_scale = (float*)" << y_scale->cname() << ";" << std::endl;
INDT_1 << "uint8_t *y_zero_point = (uint8_t*)" << y_zero_point->cname() << ";" << std::endl;
INDT_1 << "float *in_data = (float*)x;" << std::endl;
INDT_1 << "uint8_t *out_data = (uint8_t*)y;" << std::endl;
INDT_1 << "float *y_scale_ = (float*)y_scale;" << std::endl;
INDT_1 << "uint8_t *y_zero_point_ = (uint8_t*)y_zero_point;" << std::endl;
INDT_1 << "float min, max; min=max=0.0;" << std::endl;

INDT_1 << "for (int i=0; i<" << n_el << "; i++ ) {" << std::endl;
Expand All @@ -57,7 +40,7 @@ class DynamicQuantizeLinear : public Node {
INDT_1 << "}" << std::endl;

// TODO: assert output is uint8. Reading between the lines says this will change in the future
INDT_1 << "*y_scale = (max-min)/255;" << std::endl;
INDT_1 << "*y_scale_ = (max-min)/255;" << std::endl;

INDT_1 << "float fl_zero_point = (0 - min) / *y_scale;" << std::endl;
// specs say:
Expand All @@ -68,7 +51,7 @@ class DynamicQuantizeLinear : public Node {
// round() should be good enough
INDT_1 << "fl_zero_point = fl_zero_point < 0 ? 0 : fl_zero_point;" << std::endl;
INDT_1 << "fl_zero_point = fl_zero_point > 255 ? 255 : fl_zero_point;" << std::endl;
INDT_1 << "*y_zero_point = round(fl_zero_point);" << std::endl;
INDT_1 << "*y_zero_point_ = round(fl_zero_point);" << std::endl;


INDT_1 << "for (int i=0; i<" << n_el << "; i++ ) {" << std::endl;
Expand All @@ -82,25 +65,23 @@ class DynamicQuantizeLinear : public Node {

virtual void resolve(void) override
{
x = inputs[0];
const Tensor *x = inputs[0];
register_input(x, "x");

Tensor *t = new Tensor;
t->data_dim = x->data_dim;
t->data_type = onnx::TensorProto_DataType_UINT8;
y = t;
outputs.push_back(t);
register_output(t, "y");

t = new Tensor;
t->data_dim.push_back(1);
t->data_type = onnx::TensorProto_DataType_FLOAT;
y_scale = t;
outputs.push_back(t);
register_output(t, "y_scale");

t = new Tensor;
t->data_dim.push_back(1);
t->data_type = onnx::TensorProto_DataType_UINT8;
y_zero_point = t;
outputs.push_back(t);
register_output(t, "y_zero_point");
}
};
}
Expand Down
21 changes: 6 additions & 15 deletions src/nodes/elementwise.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
namespace toC {

class Elementwise : public Node {
const Tensor *X;
const Tensor *Y;
float alpha, beta, bias, gamma, lambd;

public:
Elementwise(std::string op) {
op_name = op;
X=Y=NULL;
alpha=beta=gamma=bias=0;
lambd=0.5;

Expand Down Expand Up @@ -157,13 +154,6 @@ class Elementwise : public Node {
std::function<const std::string (const std::string & Xidx)> operation =
[](const std::string& x){ ERROR("onnx2c internal error"); return ""; };

virtual void print_parameters(std::ostream &dst, bool decorate ) const override
{
X->print_tensor_as_const(dst, !decorate);

dst << ", ";
Y->print_tensor(dst, !decorate);
}

// NB: not all ONNX operators implemented with Elementwise have attributes.
// This gets the attributes over an union of all implemented operators
Expand All @@ -189,15 +179,16 @@ class Elementwise : public Node {

virtual void print(std::ostream &dst) const override
{
const Tensor *Y = outputs[0];
INDT_1 << "/* " << op_name << std::endl;
INDT_1 << " alpha = " << alpha << std::endl;
INDT_1 << " beta = " << beta << std::endl;
INDT_1 << "*/" << std::endl;

// print out the loops over all C dimensions.
// at the same time, create the indexing strings into X and Y
std::string Xidx = X->cname();
std::string Yidx = Y->cname();
std::string Xidx = "X";
std::string Yidx = "Y";
for( unsigned r=0; r< Y->rank(); r++) {
std::string lv = "i" + std::to_string(r);
INDT_1 << "for (unsigned " << lv << "=0; " << lv << "<" << Y->data_dim[r] << "; " << lv << "++) {" << std::endl;
Expand All @@ -216,13 +207,13 @@ class Elementwise : public Node {

virtual void resolve(void) override
{
X = inputs[0];
const Tensor *X = inputs[0];
register_input(X, "X");

Tensor *t = new Tensor;
t->data_dim = X->data_dim;
t->data_type = X->data_type;
Y = t;
outputs.push_back(t);
register_output(t, "Y");
}
};
}
Expand Down
26 changes: 8 additions & 18 deletions src/nodes/elementwise_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ class Elementwise_2 : public Node {
ERROR("Elementwise_2 operand " + op + " not implemented");
}

virtual void print_parameters(std::ostream &dst, bool decorate ) const override
{
inputs[0]->print_tensor_as_const(dst, !decorate);

dst << ", ";
inputs[1]->print_tensor_as_const(dst, !decorate);

dst << ", ";
outputs[0]->print_tensor(dst, !decorate);
}

virtual void parseAttributes( onnx::NodeProto &node ) override {
for( const auto& a : node.attribute() ) {
LOG(TRACE) << "Parsing attribute " << a.name() << std::endl;
Expand All @@ -125,7 +114,6 @@ class Elementwise_2 : public Node {
}
}


virtual void print(std::ostream &dst) const override
{
INDT_1 << "/* " << op_name << std::endl;
Expand All @@ -152,9 +140,9 @@ class Elementwise_2 : public Node {

// print out the loops over all C dimensions.
// at the same time, create the indexing strings into A and B
std::string Aidx = A->cname();
std::string Bidx = B->cname();
std::string Cidx = C->cname();
std::string Aidx = "A";
std::string Bidx = "B";
std::string Cidx = "C";
for( unsigned r=0; r<C->rank(); r++) {
std::string lv = "i" + std::to_string(r);
INDT_1 << "for (unsigned " << lv << "=0; " << lv << "<" << C->data_dim[r] << "; " << lv << "++) {" << std::endl;
Expand Down Expand Up @@ -190,8 +178,10 @@ class Elementwise_2 : public Node {

virtual void resolve(void) override
{
Tensor *A = inputs[0];
Tensor *B = inputs[1];
const Tensor *A = inputs[0];
const Tensor *B = inputs[1];
register_input(A, "A");
register_input(B, "B");

std::vector<int> result_dim;
multidirectional_broadcast_size(A->data_dim, B->data_dim, result_dim);
Expand All @@ -202,7 +192,7 @@ class Elementwise_2 : public Node {
t->data_type = onnx::TensorProto_DataType_BOOL;
else
t->data_type = A->data_type;
outputs.push_back(t);
register_output(t, "C");
}
};
}
Expand Down
Loading

0 comments on commit b382b82

Please sign in to comment.