SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
smv_test_common.cpp
1 #include <random>
2 
3 #include "catch.hpp"
5 #include "smaug/operators/smv/smv_test_common.h"
6 
7 namespace smaug {
8 
9 constexpr float kMean = 0.0;
10 constexpr float kVar = 0.1;
11 std::default_random_engine generator;
12 std::normal_distribution<float> normalDist(kMean, kVar);
13 constexpr float kFraction = 0.1;
14 
15 // This fills the tensor with a normal distribution of random values.
17  float16* dataPtr = tensor->data<float16>();
18  for (int i = 0; i < tensor->getShape().storageSize(); i++)
19  dataPtr[i] = fp16(normalDist(generator));
20 }
21 
23  const TensorShape& shape = tensor->getShape();
24  // Each dimension C is initialized to a different constant value.
25  float16* dataPtr = tensor->data<float16>();
26  int resetCounter = shape.getStorageDim(shape.ndims() - 1);
27  int value = 0;
28  for (int i = 0; i < shape.storageSize(); i++) {
29  dataPtr[i] = fp16((value++) * kFraction);
30  if ((i + 1) % resetCounter == 0)
31  value = 0;
32  }
33 }
34 
35 void verifyTensorWithFixedData(Tensor* tensor, int valueOffset) {
36  const TensorShape& shape = tensor->getShape();
37  float16* dataPtr = tensor->data<float16>();
38  int expectedValue = valueOffset;
39  int resetCounter = tensor->getShape().getStorageDim(shape.ndims() - 1);
40  int totalSize = tensor->getShape().storageSize();
41  for (int i = 0; i < totalSize; i++) {
42  REQUIRE(Approx(fp32(dataPtr[i])).epsilon(kEpsilon) ==
43  expectedValue * kFraction);
44  ++expectedValue;
45  if ((i + 1) % resetCounter == 0)
46  expectedValue = valueOffset;
47  }
48 }
49 
50 } // namespace smaug
smaug::Tensor
Tensor represents a single multi-dimensional array of data.
Definition: tensor.h:344
smaug::Tensor::data
const T * data() const
Returns a const pointer to the Tensor data.
Definition: tensor.h:521
smaug::verifyTensorWithFixedData
void verifyTensorWithFixedData(Tensor *tensor, int valueOffset)
Verify that the provided Tensor's data matches the fixed pattern produced by fillTensorWithFixedData,...
Definition: smv_test_common.cpp:35
smaug::fp32
float fp32(float16 fp16_data)
This converts a float16 into a float32.
smaug::TensorShape::getStorageDim
int getStorageDim(int index) const
Returns the alignment-padded size of the specified dimension.
Definition: tensor.h:69
smaug::TensorShape
TensorShape describes the shape of a Tensor.
Definition: tensor.h:35
smaug_test.h
SMAUG unit test fixture.
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::fillTensorWithRandomData
void fillTensorWithRandomData(Tensor *tensor)
This fills the Tensor with normally distributed random values.
Definition: smv_test_common.cpp:16
smaug::fillTensorWithFixedData
void fillTensorWithFixedData(Tensor *tensor)
This fills the Tensor with a fixed data pattern.
Definition: smv_test_common.cpp:22
smaug::fp16
float16 fp16(float fp32_data)
This converts a float32 into a float16.