Skip to content

Commit

Permalink
[Test] Generate TensorV2 in unit test
Browse files Browse the repository at this point in the history
This PR includes the implementation of test util functions to create a tensor filled with values to utilize in unit testing.

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghyeon Jeong <[email protected]>
  • Loading branch information
djeong20 committed Jan 22, 2024
1 parent 07db2d5 commit 114c4cb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/include/nntrainer_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <nntrainer_log.h>
#include <realizer.h>
#include <tensor.h>
#include <tensor_v2.h>

/** tolerance is reduced for packaging, but CI runs at full tolerance */
#ifdef REDUCE_TOLERANCE
Expand Down Expand Up @@ -155,6 +156,31 @@ randUniform(unsigned int batch, unsigned channel, unsigned height,
nntrainer::Tformat fm = nntrainer::Tformat::NCHW,
nntrainer::Tdatatype d_type = nntrainer::Tdatatype::FP32);

/**
* @brief return a tensor filled with contant value with dimension
*/
nntrainer::TensorV2
constantV2(float value, unsigned int d0, unsigned d1, unsigned d2, unsigned d3,
nntrainer::Tformat fm = nntrainer::Tformat::NCHW,
nntrainer::Tdatatype d_type = nntrainer::Tdatatype::FP32);

/**
* @brief return a tensor filled with ranged value with given dimension
*/
nntrainer::TensorV2
rangedV2(unsigned int batch, unsigned channel, unsigned height, unsigned width,
nntrainer::Tformat fm = nntrainer::Tformat::NCHW,
nntrainer::Tdatatype d_type = nntrainer::Tdatatype::FP32);

/**
* @brief return a tensor filled with random value with given dimension
*/
nntrainer::TensorV2
randUniformV2(unsigned int batch, unsigned channel, unsigned height,
unsigned width, float min = -1, float max = 1,
nntrainer::Tformat fm = nntrainer::Tformat::NCHW,
nntrainer::Tdatatype d_type = nntrainer::Tdatatype::FP32);

/**
* @brief replace string and save in file
* @param[in] from string to be replaced
Expand Down
39 changes: 39 additions & 0 deletions test/nntrainer_test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,45 @@ nntrainer::Tensor randUniform(unsigned int batch, unsigned int channel,
return t;
}

nntrainer::TensorV2 constantV2(float value, unsigned int d0, unsigned int d1,
unsigned int d2, unsigned int d3,
nntrainer::Tformat fm,
nntrainer::Tdatatype d_type) {
nntrainer::TensorV2 t(d0, d1, d2, d3, {fm, d_type});
t.setValue(value);

return t;
}

nntrainer::TensorV2 rangedV2(unsigned int batch, unsigned int channel,
unsigned int height, unsigned int width,
nntrainer::Tformat fm,
nntrainer::Tdatatype d_type) {
nntrainer::TensorV2 t(batch, channel, height, width, {fm, d_type});
if (d_type == nntrainer::Tdatatype::FP32) {
float i = 0;
t = t.apply((std::function<float(float)>)[&](float in) { return i++; });
} else if (d_type == nntrainer::Tdatatype::FP16) {
#ifdef ENABLE_FP16
_FP16 i = 0;
t = t.apply((std::function<_FP16(_FP16)>)[&](_FP16 in) { return i++; });
#else
throw std::invalid_argument("Error: enable-fp16 is not enabled");
#endif
}

return t;
}

nntrainer::TensorV2 randUniformV2(unsigned int batch, unsigned int channel,
unsigned int height, unsigned int width,
float min, float max, nntrainer::Tformat fm,
nntrainer::Tdatatype d_type) {
nntrainer::TensorV2 t(batch, channel, height, width, {fm, d_type});
t.setRandUniform(min, max);
return t;
}

const std::string
getResPath(const std::string &filename,
const std::initializer_list<const char *> fallback_base) {
Expand Down

0 comments on commit 114c4cb

Please sign in to comment.