1 #ifndef _OPERATORS_PADDING_OP_H_
2 #define _OPERATORS_PADDING_OP_H_
4 #include "smaug/core/backend.h"
5 #include "smaug/core/operator.h"
6 #include "smaug/core/tensor.h"
7 #include "smaug/core/workspace.h"
8 #include <google/protobuf/repeated_field.h>
9 using namespace google::protobuf;
20 template <
typename Backend>
21 class PaddingOp :
public Operator {
23 PaddingOp(
const std::string& name, Workspace* workspace)
24 : Operator(name, OpType::Padding, workspace) {
25 inputs.resize(kNumInputs,
nullptr);
26 outputs.resize(kNumOutputs,
nullptr);
35 paddingSize.assign(val.begin(), val.end());
38 void setPaddingSize(std::vector<int>
const& val) { paddingSize = val; }
40 const std::vector<int>& getPaddingSize()
const {
return paddingSize; }
43 Tensor* input = getInput(kInput);
44 Tensor* output = getOutput(kOutput);
45 int ndims = input->ndims();
46 const std::vector<int>& inputDims = input->getShape().dims();
47 const std::vector<int>& outputDims = output->getShape().dims();
49 for (
int i : outputDims) {
52 std::vector<float> vf(total_dim, 0);
53 output->fillData(vf.data(), vf.size());
54 std::vector<int> paddingBegin, srcOrigin;
55 for (
int i = 0; i < ndims; i++) {
56 paddingBegin.push_back(paddingSize.at(2 * i));
57 srcOrigin.push_back(0);
63 void createAllTensors()
override {
64 Tensor* input = getInput(kInput);
65 int ndims = input->ndims();
66 std::vector<int> dims = input->getShape().dims();
67 for (
int i = 0; i < ndims; i++) {
68 dims[i] += (paddingSize[2 * i] + paddingSize[2 * i + 1]);
71 dims, input->getShape().getLayout(), Backend::Alignment);
72 Tensor* output =
new Tensor(name, shape);
73 workspace->addTensor(output);
74 outputs.at(kOutput) = output;
78 bool validate()
override {
79 Tensor* input = getInput(kInput);
80 int ndims = input->ndims();
81 if (paddingSize.size() != 2 * ndims) {
84 return Operator::validate();
87 enum { kInput, kNumInputs };
88 enum { kOutput, kNumOutputs };
91 std::vector<int> paddingSize = {};