SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
reshape_op.h
1 #ifndef _OPERATORS_RESHAPE_OP_H_
2 #define _OPERATORS_RESHAPE_OP_H_
3 
4 #include "smaug/core/backend.h"
5 #include "smaug/core/operator.h"
7 
8 namespace smaug {
9 
20 template <typename Backend>
21 class ReshapeOp : public Operator {
22  public:
23  ReshapeOp(const std::string& name, Workspace* workspace)
24  : Operator(name, OpType::Reshape, workspace) {
25  inputs.resize(1, nullptr);
26  outputs.resize(1, nullptr);
27  }
28 
29  ReshapeOp(const std::string& name,
30  Workspace* workspace,
31  const std::vector<int>& _shape,
32  DataLayout _layout)
33  : Operator(name, OpType::Reshape, workspace), shape(_shape),
34  layout(_layout) {
35  inputs.resize(1, nullptr);
36  outputs.resize(1, nullptr);
37  }
38 
40  void setShape(const std::vector<int>& _shape, DataLayout _layout) {
41  shape = _shape;
42  layout = _layout;
43  }
45  void setShape(const std::initializer_list<int>& _shape,
46  DataLayout _layout) {
47  shape = _shape;
48  layout = _layout;
49  }
50 
51  void createAllTensors() override {
52  Tensor* input = getInput(0);
53  Tensor* output = new Tensor(
54  name, TensorShape(shape, layout, Backend::Alignment));
55  workspace->addTensor(output);
56  outputs.at(0) = output;
57  }
58 
59  void run() override {
60  // Copy the input data.
61  Tensor* input = getInput(0);
62  Tensor* output = getOutput(0);
63  const TensorShape& inputShape = input->getShape();
64  const TensorShape& outputShape = output->getShape();
65  int inputNumDims = input->ndims();
66  int outputNumDims = output->ndims();
67  int inputPadding = inputShape.getPadding(inputNumDims - 1);
68  int outputPadding = outputShape.getPadding(outputNumDims - 1);
69  if (inputPadding == outputPadding) {
70  // If input and output have the same padding, then copy the raw
71  // tensor data.
72  // TODO(xyzsam): Must also check for DataLayout here.
73  copyRawTensorData(output, input, 0, 0, inputShape.storageSize());
74  } else {
75  // Due to different paddings, we can't copy the data in one shot.
76  copyTensorData(output,
77  input,
78  std::vector<int>(outputNumDims, 0),
79  std::vector<int>(inputNumDims, 0),
80  inputShape.size());
81  }
82  }
83 
84  protected:
85  std::vector<int> shape;
86  DataLayout layout;
87 };
88 
89 } // namespace smaug
90 
91 #endif
smaug::Tensor
Tensor represents a single multi-dimensional array of data.
Definition: tensor.h:344
smaug::ReshapeOp::setShape
void setShape(const std::initializer_list< int > &_shape, DataLayout _layout)
Set the desired shape of the output.
Definition: reshape_op.h:45
smaug::copyTensorData
void copyTensorData(Tensor *dest, Tensor *src, std::vector< int > destOrigin, std::vector< int > srcOrigin, int copySize)
Similar to copyTensorRegion, but the region is a contiguous block of memory.
Definition: tensor_utils.cpp:102
tensor_utils.h
Utility functions for copying/printing/tiling tensors.
smaug::copyRawTensorData
void copyRawTensorData(Tensor *dest, Tensor *src, int destOffset, int srcOffset, int copySize)
Directly copies a linear region of memory from dest to src, without taking dimensions/padding into ac...
Definition: tensor_utils.cpp:138
smaug::TensorShape
TensorShape describes the shape of a Tensor.
Definition: tensor.h:35
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::ReshapeOp::setShape
void setShape(const std::vector< int > &_shape, DataLayout _layout)
Set the desired shape of the output.
Definition: reshape_op.h:40