36 ASSERT(paddingsShape.
type == OperandType::TENSOR_INT32);
41 std::vector<uint32_t> outDims(numInputDims);
42 for (uint32_t i = 0; i < numInputDims; ++i)
44 int32_t beforePadding = *paddingsData++;
45 int32_t afterPadding = *paddingsData++;
47 ASSERT(beforePadding >= 0 && afterPadding >= 0);
50 output->type = input.type;
51 output->dimensions = outDims;
52 output->offset = input.offset;
53 output->scale = input.scale;
63inline void Pad(
const T* input_data,
const Dims<4>& input_dims,
64 const std::vector<int>& left_paddings,
65 const std::vector<int>& right_paddings, T* output_data,
67 const int output_batch =
ArraySize(output_dims, 3);
68 const int output_height =
ArraySize(output_dims, 2);
69 const int output_width =
ArraySize(output_dims, 1);
70 const int output_depth =
ArraySize(output_dims, 0);
72 const int left_b_padding = left_paddings[3];
73 const int left_h_padding = left_paddings[2];
74 const int left_w_padding = left_paddings[1];
75 const int left_d_padding = left_paddings[0];
77 const int right_b_padding = right_paddings[3];
78 const int right_h_padding = right_paddings[2];
79 const int right_w_padding = right_paddings[1];
80 const int right_d_padding = right_paddings[0];
82 const int input_depth =
ArraySize(input_dims, 0);
84 if (left_b_padding != 0)
86 memset(output_data, 0, left_b_padding * output_height * output_width * output_depth *
89 for (
int out_b = left_b_padding; out_b < output_batch - right_b_padding; ++out_b)
91 if (left_h_padding != 0)
93 memset(output_data +
Offset(output_dims, 0, 0, 0, out_b), 0,
94 left_h_padding * output_width * output_depth *
sizeof(T));
96 for (
int out_h = left_h_padding; out_h < output_height - right_h_padding; ++out_h)
98 if (left_w_padding != 0)
100 memset(output_data +
Offset(output_dims, 0, 0, out_h, out_b), 0,
101 left_w_padding * output_depth *
sizeof(T));
103 for (
int out_w = left_w_padding; out_w < output_width - right_w_padding; ++out_w)
105 if (left_d_padding != 0)
107 memset(output_data +
Offset(output_dims, 0, out_w, out_h, out_b), 0,
108 left_d_padding *
sizeof(T));
112 Offset(output_dims, left_d_padding, out_w, out_h, out_b);
115 out_h - left_h_padding, out_b - left_b_padding);
116 memcpy(out, in, input_depth *
sizeof(T));
118 if (right_d_padding != 0)
121 output_data +
Offset(output_dims, output_depth - right_d_padding,
122 out_w, out_h, out_b),
123 0, right_d_padding *
sizeof(T));
126 if (right_w_padding != 0)
129 output_data +
Offset(output_dims, 0, output_width - right_w_padding,
131 0, right_w_padding * output_depth *
sizeof(T));
134 if (right_h_padding != 0)
136 memset(output_data +
Offset(output_dims, 0, 0,
137 output_height - right_h_padding, out_b),
138 0, right_h_padding * output_width * output_depth *
sizeof(T));
141 if (right_b_padding != 0)
144 Offset(output_dims, 0, 0, 0, output_batch - right_b_padding),
146 right_b_padding * output_height * output_width * output_depth *
153bool padGeneric(
const uint8_t* inputData,
const Shape& inputShape,
const int32_t* paddings,
154 uint8_t* outputData,
const Shape& outputShape)
158 std::vector<int> beforePadding;
159 std::vector<int> afterPadding;
161 for (int32_t i = numInputDims - 1; i >= 0; --i)
163 beforePadding.push_back(paddings[i * 2]);
164 afterPadding.push_back(paddings[i * 2 + 1]);
167 if (inputShape.
type == OperandType::TENSOR_FLOAT32)
169 ::Pad(
reinterpret_cast<const float*
>(inputData),
171 beforePadding, afterPadding,
172 reinterpret_cast<float*
>(outputData),
175 else if (inputShape.
type == OperandType::TENSOR_QUANT8_ASYMM)
177 ::Pad(
reinterpret_cast<const uint8_t*
>(inputData),
179 beforePadding, afterPadding,
180 reinterpret_cast<uint8_t*
>(outputData),
185 LOG(ERROR) <<
"Unsupported data type";
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
int ArraySize(const Dims< N > &array, int index)
Dims< 4 > convertShapeToDims(const Shape &shape)
uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx)
uint32_t getNumberOfDimensions(const Shape &shape)
bool padGeneric(const uint8_t *inputData, const Shape &inputShape, const int32_t *paddings, uint8_t *outputData, const Shape &outputShape)
bool padPrepare(const Shape &input, const int32_t *paddingsData, const Shape &paddingsShape, Shape *output)
void Pad(const int32_t *padding_data, int32_t pad_rank, const Shape &input_shape, const T *input_data, const Shape &output_shape, T *output_data, const T *constant_value_data)