SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
operator.h
1 #ifndef _CORE_OPERATOR_H_
2 #define _CORE_OPERATOR_H_
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <boost/format.hpp>
8 
9 #include "smaug/core/typedefs.h"
10 #include "smaug/core/tensor.h"
12 #include "smaug/core/types.pb.h"
13 #include "smaug/operators/common.h"
14 
15 #define REGISTER_SPECIAL_OP(Operator, Backend) \
16  template <> \
17  void Operator<Backend>::run();
18 
19 namespace smaug {
20 
21 class Workspace;
22 
23 constexpr const char* kLayerFormat = "%-40s %-25s %=15d\n";
24 
28 class Operator {
29  public:
30  Operator(const std::string& _name, OpType _opType, Workspace* _workspace)
31  : name(_name), opType(_opType), workspace(_workspace),
32  numPendingInputs(-1) {}
33  virtual ~Operator() {}
34 
35  virtual void tile() {};
36 
42  virtual void run() = 0;
43 
47  virtual bool validate() {
48  return opType != OpType::UnknownOp;
49  }
50 
70  virtual void createAllTensors() {}
71 
78  virtual bool isDead();
79 
86  virtual std::vector<TensorBase*> getParameterizableInputs() { return {}; }
87 
89  virtual int getNumParameters() const { return 0; }
90  virtual bool isSamplingSupported() const { return false; }
91  virtual void setSamplingInfo(const SamplingInfo& sampling) {}
92 
93  void printSummary(std::ostream& out) const;
94  void setInput(TensorBase* op, int index) { inputs[index] = op; }
95  void setOutput(TensorBase* op, int index) { outputs[index] = op; }
96 
101  void setNumPendingInputs(int num) { numPendingInputs = num; }
102  int getNumPendingInputs() const { return numPendingInputs; }
103  void decrNumPendingInputs() { numPendingInputs--; }
104  const std::string& getName() const { return name; }
105  Vertex getVertex() const { return vertex; }
106  void setVertex(Vertex v) { vertex = v; }
107  OpType getOpType() const { return opType; }
108  Workspace* getWorkspace() { return workspace; }
109 
110  Tensor* getInput(int index) const {
111  return dynamic_cast<Tensor*>(inputs.at(index));
112  }
113  const std::vector<TensorBase*>& getInputs() const { return inputs; }
114 
115  Tensor* getOutput(int index) const {
116  return dynamic_cast<Tensor*>(outputs.at(index));
117  }
118  const std::vector<TensorBase*>& getOutputs() const { return outputs; }
119 
120  void setInputsMemType(MemoryType type) { inputsMemType = type; }
121  void setWeightsMemType(MemoryType type) { weightsMemType = type; }
122  void setOutputsMemType(MemoryType type) { outputsMemType = type; }
123  MemoryType getInputsMemType() const { return inputsMemType; }
124  MemoryType getWeightsMemType() const { return weightsMemType; }
125  MemoryType getOutputsMemType() const { return outputsMemType; }
126 
127  protected:
134  std::vector<TensorBase*> inputs;
135 
141  std::vector<TensorBase*> outputs;
142 
143  std::string name;
144  OpType opType;
146  Vertex vertex;
147  Workspace* workspace;
152  MemoryType inputsMemType;
154  MemoryType weightsMemType;
156  MemoryType outputsMemType;
157 };
158 
159 } // namespace smaug
160 
161 #endif
_SamplingInfo
Simulation sampling information maintained by the Operator and passed to the accelerated kernel.
Definition: common.h:262
smaug::Operator::numPendingInputs
int numPendingInputs
The number of tensors that this operator is waiting on before it can be scheduled.
Definition: operator.h:150
smaug::Operator::getNumParameters
virtual int getNumParameters() const
This returns the number of parameterizable weights in the operator.
Definition: operator.h:89
smaug::Operator::outputsMemType
MemoryType outputsMemType
The memory interface over which outputs are expected to be delivered.
Definition: operator.h:156
smaug::Operator::inputsMemType
MemoryType inputsMemType
The memory interface over which input activations are expected to arrive.
Definition: operator.h:152
smaug::Workspace
Workspace is the container and owner of all Tensors and Operators in the Network.
Definition: workspace.h:17
smaug::Operator::createAllTensors
virtual void createAllTensors()
For tests: creates all input and output tensors for this operator.
Definition: operator.h:70
tensor_utils.h
Utility functions for copying/printing/tiling tensors.
smaug::Operator::getParameterizableInputs
virtual std::vector< TensorBase * > getParameterizableInputs()
Return a list of Tensors whose values that are parameterizable.
Definition: operator.h:86
smaug::Operator::setNumPendingInputs
void setNumPendingInputs(int num)
Set the number of input tensors that this operator is waiting on.
Definition: operator.h:101
smaug::Operator::outputs
std::vector< TensorBase * > outputs
An ordered list of output tensors produced by this operator.
Definition: operator.h:141
smaug::Operator::vertex
Vertex vertex
The BGL Vertex corresponding to this Operator.
Definition: operator.h:146
smaug::Operator::run
virtual void run()=0
Executes the Operator.
smaug::Operator
Operator is the base class for all graph operators supported by SMAUG.
Definition: operator.h:28
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
common.h
Utilities for writing and invoking Aladdin kernels from Operators.
smaug::Operator::validate
virtual bool validate()
Returns true if the parameters/tensors of this operator are all valid.
Definition: operator.h:47
smaug::Operator::weightsMemType
MemoryType weightsMemType
The memory interface over which weights are expected to arrive.
Definition: operator.h:154
smaug::Operator::inputs
std::vector< TensorBase * > inputs
An ordered list of input tensors consumed by this operator.
Definition: operator.h:134
smaug::Operator::isDead
virtual bool isDead()
Returns true if the Operator is dead.
Definition: operator.cpp:5