ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Pad.cpp File Reference
#include "Pad.h"
#include "Assert.h"
#include "Logging.h"
#include "internal/Dims.h"
#include <vector>
#include <cstring>

Go to the source code of this file.

Functions

bool padPrepare (const Shape &input, const int32_t *paddingsData, const Shape &paddingsShape, Shape *output)
 
bool padGeneric (const uint8_t *inputData, const Shape &inputShape, const int32_t *paddings, uint8_t *outputData, const Shape &outputShape)
 

Function Documentation

◆ padGeneric()

bool padGeneric ( const uint8_t *  inputData,
const Shape inputShape,
const int32_t *  paddings,
uint8_t *  outputData,
const Shape outputShape 
)

Definition at line 153 of file Pad.cpp.

155{
156 int32_t numInputDims = static_cast<int32_t>(getNumberOfDimensions(inputShape));
157
158 std::vector<int> beforePadding;
159 std::vector<int> afterPadding;
160 // The lower level implementation expects the paddings in the reverse order.
161 for (int32_t i = numInputDims - 1; i >= 0; --i)
162 {
163 beforePadding.push_back(paddings[i * 2]);
164 afterPadding.push_back(paddings[i * 2 + 1]);
165 }
166
167 if (inputShape.type == OperandType::TENSOR_FLOAT32)
168 {
169 ::Pad(reinterpret_cast<const float*>(inputData),
170 convertShapeToDims(inputShape),
171 beforePadding, afterPadding,
172 reinterpret_cast<float*>(outputData),
173 convertShapeToDims(outputShape));
174 }
175 else if (inputShape.type == OperandType::TENSOR_QUANT8_ASYMM)
176 {
177 ::Pad(reinterpret_cast<const uint8_t*>(inputData),
178 convertShapeToDims(inputShape),
179 beforePadding, afterPadding,
180 reinterpret_cast<uint8_t*>(outputData),
181 convertShapeToDims(outputShape));
182 }
183 else
184 {
185 LOG(ERROR) << "Unsupported data type";
186 return false;
187 }
188 return true;
189}
Dims< 4 > convertShapeToDims(const Shape &shape)
Definition Dims.h:31
#define LOG(...)
Definition Logging.h:36
uint32_t getNumberOfDimensions(const Shape &shape)
Definition Shape.cpp:58
OperandType type
Definition Shape.h:29

References convertShapeToDims(), getNumberOfDimensions(), LOG, and Shape::type.

◆ padPrepare()

bool padPrepare ( const Shape input,
const int32_t *  paddingsData,
const Shape paddingsShape,
Shape output 
)

Definition at line 28 of file Pad.cpp.

30{
31 // Currently only 4D tensors are supported.
32 uint32_t numInputDims = getNumberOfDimensions(input);
33 ASSERT(numInputDims == 4);
34
35 // paddings need to be provided as a 2-D int32 tensor.
36 ASSERT(paddingsShape.type == OperandType::TENSOR_INT32);
37 ASSERT(getNumberOfDimensions(paddingsShape) == 2);
38 ASSERT(getSizeOfDimension(paddingsShape, 0) == numInputDims);
39 ASSERT(getSizeOfDimension(paddingsShape, 1) == 2);
40
41 std::vector<uint32_t> outDims(numInputDims);
42 for (uint32_t i = 0; i < numInputDims; ++i)
43 {
44 int32_t beforePadding = *paddingsData++;
45 int32_t afterPadding = *paddingsData++;
46 // Pad value has to be greater than equal to 0.
47 ASSERT(beforePadding >= 0 && afterPadding >= 0);
48 outDims[i] = beforePadding + getSizeOfDimension(input, i) + afterPadding;
49 }
50 output->type = input.type;
51 output->dimensions = outDims;
52 output->offset = input.offset;
53 output->scale = input.scale;
54
55 return true;
56}
#define ASSERT(v)
Definition Assert.h:24
uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx)
Definition Shape.cpp:60

References ASSERT, getNumberOfDimensions(), getSizeOfDimension(), and Shape::type.