Skip to content

Commit 7108b7b

Browse files
ConvTranspose2d Operator (#91)
Add ConvTranspose2d operator
1 parent 52b2eb4 commit 7108b7b

10 files changed

+779
-3
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//this file was generated by ../../../../../../scripts/onnx_generator/OperatorTemplate.py
2+
#include "operator__ai_onnx__convtranspose__11.h"
3+
#include "tracing.h"
4+
#include "utils.h"
5+
6+
//transformes a 3d pos into a 1d flat array pos
7+
static inline int calcArrayPos3D(int x, int y, int outputChannel, int width, int height) {
8+
return outputChannel * height * width + y * width + x;
9+
}
10+
11+
//transformes a 4d pos into a 1d flat array pos
12+
static inline int calcArrayPos4D(int x, int y, int outputChannel, int intputChannel, int width, int height, int nOfOutputChannels) {
13+
return intputChannel * nOfOutputChannels *height * width
14+
+ outputChannel * height * width
15+
+ y * width
16+
+ x;
17+
}
18+
19+
operator_status
20+
execute_operator__ai_onnx__convtranspose__11__T_tensor_float(
21+
node_context *ctx
22+
)
23+
{
24+
TRACE_ENTRY(1);
25+
26+
TRACE_NODE(2, true, ctx->onnx_node);
27+
28+
/* UNCOMMENT AS NEEDED */
29+
30+
Onnx__TensorProto *i_X = searchInputByName(ctx, 0);
31+
Onnx__TensorProto *i_W = searchInputByName(ctx, 1);
32+
Onnx__TensorProto *i_B = searchInputByName(ctx, 2);
33+
34+
TRACE_TENSOR(2, true, i_X);
35+
TRACE_TENSOR(2, true, i_W);
36+
TRACE_TENSOR(2, B, i_B);
37+
38+
context_operator__ai_onnx__convtranspose__11 *op_ctx = ctx->executer_context;
39+
40+
// size is not needed, because this operator works for one fixed size only
41+
42+
// char* auto_pad = op_ctx->auto_pad;
43+
// size_t n_dilations = op_ctx->n_dilations;
44+
int64_t* dilations = op_ctx->dilations;
45+
// int64_t group = op_ctx->group;
46+
// size_t n_kernel_shape = op_ctx->n_kernel_shape;
47+
// int64_t* kernel_shape = op_ctx->kernel_shape;
48+
// size_t n_output_padding = op_ctx->n_output_padding;
49+
// int64_t* output_padding = op_ctx->output_padding;
50+
// size_t n_output_shape = op_ctx->n_output_shape;
51+
// int64_t* output_shape = op_ctx->output_shape;
52+
// size_t n_pads = op_ctx->n_pads;
53+
int64_t* pads = op_ctx->pads;
54+
// size_t n_strides = op_ctx->n_strides;
55+
int64_t* strides = op_ctx->strides;
56+
57+
// TRACE_VAR(2, true, auto_pad, "\"%s\"");
58+
TRACE_ARRAY(2, true, dilations, , n_dilations, "%" PRId64);
59+
// TRACE_VAR(2, true, group, "%" PRId64);
60+
// TRACE_ARRAY(2, true, kernel_shape, , n_kernel_shape, "%" PRId64);
61+
// TRACE_ARRAY(2, true, output_padding, , n_output_padding, "%" PRId64);
62+
// TRACE_ARRAY(2, true, output_shape, , n_output_shape, "%" PRId64);
63+
TRACE_ARRAY(2, true, pads, , n_pads, "%" PRId64);
64+
TRACE_ARRAY(2, true, strides, , n_strides, "%" PRId64);
65+
66+
Onnx__TensorProto *o_Y = searchOutputByName(ctx, 0);
67+
68+
// TRACE_TENSOR(2, true, o_Y);
69+
70+
/* DO CALCULATION HERE */
71+
72+
const int inputSizeX = i_X->dims[3];
73+
const int inputSizeY = i_X->dims[2];
74+
const int inputChannels = i_X->dims[1];
75+
76+
const float *input = i_X->float_data;
77+
const float *weights = i_W->float_data;
78+
79+
const int kernelSizeX = i_W->dims[3];
80+
const int kernelSizeY = i_W->dims[2];
81+
const int outputChannels = i_W->dims[1];
82+
83+
const int strideX = strides[1];
84+
const int strideY = strides[0];
85+
86+
const int dilationsX = dilations[1];
87+
const int dilationsY = dilations[0];
88+
89+
const int padStartY = pads[0];
90+
const int padStartX = pads[1];
91+
92+
//not used because outputSize is used to test for the padEnd
93+
//const int padEndY = pads[2];
94+
//const int padEndX = pads[3];
95+
96+
const int outputSizeX = o_Y->dims[2];
97+
const int outputSizeY = o_Y->dims[3];
98+
99+
float* output = o_Y->float_data;
100+
101+
//fill with bias
102+
for(int c=0; c<outputChannels; c++) {
103+
float bias = i_B?i_B->float_data[c]:0;
104+
for(int y=0; y<outputSizeY; y++) {
105+
for(int x=0; x<outputSizeX; x++) {
106+
output[calcArrayPos3D(x,y,c,outputSizeX, outputSizeY)] = bias;
107+
}
108+
}
109+
}
110+
111+
//actual transpose convolution
112+
for(int i=0; i < inputChannels; i++) {
113+
for(int c=0; c<outputChannels; c++) {
114+
for(int inputPosY=0; inputPosY<inputSizeY; inputPosY++) {
115+
for(int inputPosX=0; inputPosX<inputSizeX; inputPosX++) {
116+
float _input = input[calcArrayPos3D(inputPosX, inputPosY, i, inputSizeX, inputSizeY)];
117+
118+
for(int kernelPosX=0; kernelPosX<kernelSizeX; kernelPosX++) {
119+
int x = inputPosX*strideX+kernelPosX*dilationsX - padStartX;
120+
if(x < 0 || x >= outputSizeX) {
121+
continue;
122+
}
123+
124+
for(int kernelPosY=0; kernelPosY<kernelSizeY; kernelPosY++) {
125+
int y = inputPosY*strideY+kernelPosY*dilationsY - padStartY;
126+
if(y < 0 || y >= outputSizeY) {
127+
continue;
128+
}
129+
130+
const float _weight = weights[calcArrayPos4D(kernelPosX, kernelPosY, c, i, kernelSizeX, kernelSizeY, outputChannels)];
131+
output[calcArrayPos3D(x, y, c, outputSizeX, outputSizeY)] += _input * _weight;
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
139+
TRACE_EXIT(1);
140+
141+
/* CHANGE RETURN CODE IF THIS EXECUTER IS VALID */
142+
return OP_OK;
143+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//this file was generated by ../../../../../../scripts/onnx_generator/OperatorTemplate.py
2+
#include "operator__ai_onnx__convtranspose__11.h"
3+
#include "tracing.h"
4+
#include "utils.h"
5+
6+
void
7+
free_operator__ai_onnx__convtranspose__11(
8+
node_context *ctx
9+
)
10+
{
11+
TRACE_ENTRY(1);
12+
13+
TRACE_NODE(2, true, ctx->onnx_node);
14+
15+
/* UNCOMMENT AS NEEDED */
16+
17+
// Onnx__TensorProto *i_X = searchInputByName(ctx, 0);
18+
// Onnx__TensorProto *i_W = searchInputByName(ctx, 1);
19+
// Onnx__TensorProto *i_B = searchInputByName(ctx, 2);
20+
21+
// TRACE_TENSOR(2, true, i_X);
22+
// TRACE_TENSOR(2, true, i_W);
23+
// TRACE_TENSOR(2, B, i_B);
24+
25+
// Onnx__AttributeProto *a_auto_pad = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"auto_pad");
26+
// Onnx__AttributeProto *a_dilations = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"dilations");
27+
// Onnx__AttributeProto *a_group = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"group");
28+
// Onnx__AttributeProto *a_kernel_shape = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"kernel_shape");
29+
// Onnx__AttributeProto *a_output_padding = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"output_padding");
30+
// Onnx__AttributeProto *a_output_shape = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"output_shape");
31+
// Onnx__AttributeProto *a_pads = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"pads");
32+
// Onnx__AttributeProto *a_strides = searchAttributeNyName(ctx->onnx_node->n_attribute,ctx->onnx_node->attribute,"strides");
33+
34+
// TRACE_ATTRIBUTE(2, a_auto_pad, a_auto_pad);
35+
// TRACE_ATTRIBUTE(2, a_dilations, a_dilations);
36+
// TRACE_ATTRIBUTE(2, a_group, a_group);
37+
// TRACE_ATTRIBUTE(2, a_kernel_shape, a_kernel_shape);
38+
// TRACE_ATTRIBUTE(2, a_output_padding, a_output_padding);
39+
// TRACE_ATTRIBUTE(2, a_output_shape, a_output_shape);
40+
// TRACE_ATTRIBUTE(2, a_pads, a_pads);
41+
// TRACE_ATTRIBUTE(2, a_strides, a_strides);
42+
43+
Onnx__TensorProto *o_Y = searchOutputByName(ctx, 0);
44+
45+
// TRACE_TENSOR(2, true, o_Y);
46+
47+
/* FREE CONTEXT HERE IF NEEDED */
48+
49+
context_operator__ai_onnx__convtranspose__11 *op_ctx = ctx->executer_context;
50+
51+
// TRACE_VAR(2, true, op_ctx->auto_pad, "\"%s\"");
52+
TRACE_ARRAY(2, true, op_ctx->dilations, , op_ctx->n_dilations, "%" PRId64);
53+
// TRACE_VAR(2, true, op_ctx->group, "%" PRId64);
54+
// TRACE_ARRAY(2, true, op_ctx->kernel_shape, , op_ctx->n_kernel_shape, "%" PRId64);
55+
// TRACE_ARRAY(2, true, op_ctx->output_padding, , op_ctx->n_output_padding, "%" PRId64);
56+
// TRACE_ARRAY(2, true, op_ctx->output_shape, , op_ctx->n_output_shape, "%" PRId64);
57+
TRACE_ARRAY(2, true, op_ctx->pads, , op_ctx->n_pads, "%" PRId64);
58+
TRACE_ARRAY(2, true, op_ctx->strides, , op_ctx->n_strides, "%" PRId64);
59+
60+
// free(op_ctx->auto_pad);
61+
free(op_ctx->dilations);
62+
// free(op_ctx->kernel_shape);
63+
// free(op_ctx->output_padding);
64+
// free(op_ctx->output_shape);
65+
free(op_ctx->pads);
66+
free(op_ctx->strides);
67+
68+
free(op_ctx);
69+
70+
71+
/* FREE OUTPUT DATA_TYPE AND SHAPE HERE */
72+
/* DO NOT FREE THE TENSOR ITSELF */
73+
74+
// freeTensorData(o_Y);
75+
free(o_Y->dims);
76+
77+
TRACE_EXIT(1);
78+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//this file was generated by ../../../../../../scripts/onnx_generator/OperatorInfo.py
2+
#include "operators/operator_info.h"
3+
#include "operator__ai_onnx__convtranspose__11.h"
4+
5+
/* attributes */
6+
static
7+
operator_info_attribute
8+
attributes[] = {
9+
{
10+
.name = "auto_pad",
11+
.optional = true,
12+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__STRING
13+
},
14+
{
15+
.name = "dilations",
16+
.optional = true,
17+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
18+
},
19+
{
20+
.name = "group",
21+
.optional = true,
22+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INT
23+
},
24+
{
25+
.name = "kernel_shape",
26+
.optional = true,
27+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
28+
},
29+
{
30+
.name = "output_padding",
31+
.optional = true,
32+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
33+
},
34+
{
35+
.name = "output_shape",
36+
.optional = true,
37+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
38+
},
39+
{
40+
.name = "pads",
41+
.optional = true,
42+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
43+
},
44+
{
45+
.name = "strides",
46+
.optional = true,
47+
.type = ONNX__ATTRIBUTE_PROTO__ATTRIBUTE_TYPE__INTS
48+
}
49+
};
50+
51+
/* input tensors */
52+
static
53+
uint32_t
54+
input_tensor_type_X[] = {
55+
ONNX__TENSOR_PROTO__DATA_TYPE__DOUBLE,
56+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT,
57+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT16
58+
};
59+
60+
static
61+
uint32_t
62+
input_tensor_type_W[] = {
63+
ONNX__TENSOR_PROTO__DATA_TYPE__DOUBLE,
64+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT,
65+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT16
66+
};
67+
68+
static
69+
uint32_t
70+
input_tensor_type_B[] = {
71+
ONNX__TENSOR_PROTO__DATA_TYPE__DOUBLE,
72+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT,
73+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT16
74+
};
75+
76+
static
77+
operator_info_tensor
78+
inputs[] = {
79+
{
80+
.name = "X",
81+
.optional = false,
82+
.variadic = false,
83+
.homogeneous = true,
84+
.constraint = "T",
85+
.n_types = 3,
86+
.types = input_tensor_type_X
87+
},
88+
{
89+
.name = "W",
90+
.optional = false,
91+
.variadic = false,
92+
.homogeneous = true,
93+
.constraint = "T",
94+
.n_types = 3,
95+
.types = input_tensor_type_W
96+
},
97+
{
98+
.name = "B",
99+
.optional = true,
100+
.variadic = true,
101+
.homogeneous = true,
102+
.constraint = "T",
103+
.n_types = 3,
104+
.types = input_tensor_type_B
105+
}
106+
};
107+
108+
/* output tensors */
109+
static
110+
uint32_t
111+
output_tensor_type_Y[] = {
112+
ONNX__TENSOR_PROTO__DATA_TYPE__DOUBLE,
113+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT,
114+
ONNX__TENSOR_PROTO__DATA_TYPE__FLOAT16
115+
};
116+
117+
static
118+
operator_info_tensor
119+
outputs[] = {
120+
{
121+
.name = "Y",
122+
.optional = false,
123+
.variadic = false,
124+
.homogeneous = true,
125+
.constraint = "T",
126+
.n_types = 3,
127+
.types = output_tensor_type_Y
128+
}
129+
};
130+
131+
/* constraints */
132+
static
133+
operator_info_constraint
134+
constraints[] = {
135+
{ "T" }
136+
};
137+
138+
/* operator info */
139+
operator_info
140+
info_operator__ai_onnx__convtranspose__11 = {
141+
.name = "ConvTranspose",
142+
.range_input = { 2, 3 },
143+
.range_output = { 1, 1 },
144+
.n_attribute = 8,
145+
.attribute = attributes,
146+
.n_input = 3,
147+
.input = inputs,
148+
.n_output = 1,
149+
.output = outputs,
150+
.n_constraint = 1,
151+
.constraint = constraints
152+
};

0 commit comments

Comments
 (0)