SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
padding_op.h
1 #ifndef _OPERATORS_PADDING_OP_H_
2 #define _OPERATORS_PADDING_OP_H_
3 
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;
10 
11 namespace smaug {
12 
20 template <typename Backend>
21 class PaddingOp : public Operator {
22  public:
23  PaddingOp(const std::string& name, Workspace* workspace)
24  : Operator(name, OpType::Padding, workspace) {
25  inputs.resize(kNumInputs, nullptr);
26  outputs.resize(kNumOutputs, nullptr);
27  }
28 
34  void setPaddingSize(const RepeatedField<google::protobuf::int32>& val) {
35  paddingSize.assign(val.begin(), val.end());
36  }
37 
38  void setPaddingSize(std::vector<int> const& val) { paddingSize = val; }
39 
40  const std::vector<int>& getPaddingSize() const { return paddingSize; }
41 
42  void run() override {
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();
48  int total_dim = 1;
49  for (int i : outputDims) {
50  total_dim *= i;
51  }
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);
58  }
59  copyTensorRegion(output, input, paddingBegin, srcOrigin, inputDims);
60  }
61 
62  // Optional override for testing purposes.
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]);
69  }
70  TensorShape shape(
71  dims, input->getShape().getLayout(), Backend::Alignment);
72  Tensor* output = new Tensor(name, shape);
73  workspace->addTensor(output);
74  outputs.at(kOutput) = output;
75  }
76 
77  // Optional but recommended function to verify operator parameters.
78  bool validate() override {
79  Tensor* input = getInput(kInput);
80  int ndims = input->ndims();
81  if (paddingSize.size() != 2 * ndims) {
82  return false;
83  }
84  return Operator::validate();
85  }
86 
87  enum { kInput, kNumInputs };
88  enum { kOutput, kNumOutputs };
89 
90  private:
91  std::vector<int> paddingSize = {};
92 };
93 
94 } // namespace smaug
95 
96 #endif
smaug::PaddingOp::setPaddingSize
void setPaddingSize(const RepeatedField< google::protobuf::int32 > &val)
Set the paddingSize of the Tensor along each dimension.
Definition: padding_op.h:34
smaug::copyTensorRegion
void copyTensorRegion(Tensor *dest, Tensor *src, std::vector< int > destOrigin, std::vector< int > srcOrigin, std::vector< int > regionSize)
Copies a region of a source Tensor to a corresponding region in a destination Tensor.
Definition: tensor_utils.cpp:65
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38