SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
smaug_test.h
Go to the documentation of this file.
1 
6 #include <fstream>
7 
8 #include "catch.hpp"
9 #include "smaug/core/network.h"
10 #include "smaug/core/backend.h"
11 #include "smaug/core/tensor.h"
12 #include "smaug/core/workspace.h"
13 
14 namespace smaug {
15 
20 typedef void (*FillTensorDataFunc)(Tensor* tensor);
21 
26 constexpr float kMargin = 0.001;
27 
32 constexpr float kEpsilon = 0.01;
33 
34 class Operator;
35 
43 class SmaugTest {
44  public:
45  SmaugTest() {
46  network_ = new Network("test");
47  workspace_ = new Workspace();
48  SmvBackend::initGlobals();
49  // Set the global variables.
50  runningInSimulation = false;
53  }
54 
55  ~SmaugTest() {
56  delete network_;
57  delete workspace_;
58  SmvBackend::freeGlobals();
59  }
60 
67  template <typename T>
69  for (auto t : op->getInputs()) {
70  auto tensor = dynamic_cast<Tensor*>(t);
71  tensor->template allocateStorage<T>();
72  }
73  for (auto t : op->getOutputs()) {
74  auto tensor = dynamic_cast<Tensor*>(t);
75  tensor->template allocateStorage<T>();
76  }
77  }
78 
88  template <typename T>
90  FillTensorDataFunc fillTensorDataFunc) {
91  op->createAllTensors();
92  allocateAllTensors<T>(op);
93  for (auto input : op->getInputs()) {
94  Tensor* tensor = dynamic_cast<Tensor*>(input);
95  fillTensorDataFunc(tensor);
96  }
97  }
98 
106  template <typename DType>
107  void verifyOutputs(Tensor* output,
108  const std::vector<DType>& expected) {
109  auto ptr = output->template data<DType>();
110  int i = 0;
111  for (auto idx = output->startIndex(); !idx.end(); ++idx, ++i) {
112  REQUIRE(Approx(ptr[idx]).margin(kMargin).epsilon(kEpsilon) ==
113  expected[i]);
114  }
115  REQUIRE(i == expected.size());
116  }
117 
125  template <typename DType>
126  void verifyOutputs(Tensor* output, Tensor* expected) {
127  auto outputPtr = output->template data<DType>();
128  auto expectedPtr = expected->template data<DType>();
129  auto outputIdx = output->startIndex();
130  auto expectedIdx = expected->startIndex();
131  for (; !outputIdx.end(); ++outputIdx, ++expectedIdx) {
132  REQUIRE(Approx(outputPtr[outputIdx])
133  .margin(kMargin)
134  .epsilon(kEpsilon) == expectedPtr[expectedIdx]);
135  }
136  }
137 
138  Network* buildNetwork(const std::string& modelTopo,
139  const std::string& modelParams);
140  Tensor* buildAndRunNetwork(const std::string& modelTopo,
141  const std::string& modelParams);
142 
143  Network* network() const { return network_; }
144  Workspace* workspace() const { return workspace_; }
145 
146  protected:
151  std::string resolvePath(const std::string& relPath) {
152  const char* baseDir = std::getenv("SMAUG_HOME");
153  if (baseDir == NULL)
154  assert(false && "SMAUG_HOME is not set.");
155  std::string fullPath = std::string(baseDir) + '/' + relPath;
156  if (!std::ifstream(fullPath)) {
157  std::cerr << "File " << fullPath
158  << " doesn't exist! This could be because the proto is "
159  "too large to be submit to GitHub, please check and "
160  "create it locally.\n";
161  exit(0);
162  }
163  return fullPath;
164  }
165 
166  Network* network_;
167  Workspace* workspace_;
168 };
169 
171 float16 fp16(float fp32_data);
172 
174 float fp32(float16 fp16_data);
175 
180 Tensor* convertFp16ToFp32Tensor(Tensor* fp16Tensor, Workspace* workspace);
181 
186 Tensor* convertFp32ToFp16Tensor(Tensor* fp32Tensor, Workspace* workspace);
187 
188 } // namespace smaug
smaug::Tensor
Tensor represents a single multi-dimensional array of data.
Definition: tensor.h:344
smaug::SmaugTest
The Catch2 test fixture used by all C++ unit tests.
Definition: smaug_test.h:43
smaug::numAcceleratorsAvailable
int numAcceleratorsAvailable
The actual number of accelerator complexes currently in use.
Definition: globals.cpp:6
smaug::Tensor::startIndex
TensorIndexIterator startIndex() const
Returns an iterator starting at the beginning of the Tensor.
Definition: tensor.h:387
smaug::Network
Network encapsulates all of the information SMAUG will use during execution: the overall computation ...
Definition: network.h:39
smaug::convertFp16ToFp32Tensor
Tensor * convertFp16ToFp32Tensor(Tensor *fp16Tensor, Workspace *workspace)
This creates a tensor with float32 data type and fills it with data converted from a source tensor wi...
smaug::SmaugTest::resolvePath
std::string resolvePath(const std::string &relPath)
Resolves a SMAUG_HOME relative path to a particular resource (like a protobuf).
Definition: smaug_test.h:151
smaug::useSystolicArrayWhenAvailable
bool useSystolicArrayWhenAvailable
If true, uses the systolic array for applicable operators when backend support exists.
Definition: globals.cpp:8
smaug::fp32
float fp32(float16 fp16_data)
This converts a float16 into a float32.
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
smaug::SmaugTest::verifyOutputs
void verifyOutputs(Tensor *output, Tensor *expected)
Compares the contents of the two given Tensors against each other and asserts (REQUIRE) that the two ...
Definition: smaug_test.h:126
smaug::SmaugTest::verifyOutputs
void verifyOutputs(Tensor *output, const std::vector< DType > &expected)
Compares the contents of the given Tensor against an std::vector of data elements and asserts (REQUIR...
Definition: smaug_test.h:107
smaug::runningInSimulation
bool runningInSimulation
This is true if the user chooses to run the network in gem5 simulation.
Definition: globals.cpp:4
smaug::SmaugTest::createAndFillTensorsWithData
void createAndFillTensorsWithData(Operator *op, FillTensorDataFunc fillTensorDataFunc)
Fills every input Tensor in the Operator with data by calling the provided FillTensorDataFunc.
Definition: smaug_test.h:89
smaug::SmaugTest::allocateAllTensors
void allocateAllTensors(Operator *op)
Allocates data storage for all Tensors in the Operator.
Definition: smaug_test.h:68
smaug::Operator
Operator is the base class for all graph operators supported by SMAUG.
Definition: operator.h:28
smaug::kMargin
constexpr float kMargin
Sets the absolute value by which a result can differ from Approx's expected value.
Definition: smaug_test.h:26
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::kEpsilon
constexpr float kEpsilon
Set the percentage by which a result can differ from Approx's expected value.
Definition: smaug_test.h:32
smaug::FillTensorDataFunc
void(* FillTensorDataFunc)(Tensor *tensor)
Any function that accepts a Tensor, fills it with data, and returns nothing.
Definition: smaug_test.h:20
smaug::fp16
float16 fp16(float fp32_data)
This converts a float32 into a float16.
smaug::convertFp32ToFp16Tensor
Tensor * convertFp32ToFp16Tensor(Tensor *fp32Tensor, Workspace *workspace)
This creates a tensor with float16 data type and fills it with data converted from a source tensor wi...