Skip to content

Commit

Permalink
[tests/nnfw_api] Add epochs and fix Conv2D test (#12704)
Browse files Browse the repository at this point in the history
This commit adds epochs to CirclePlusGen and apply it to Conv2D test to
pass the test. Additionaly bias shape is fixed.

ONE-DCO-1.0-Signed-off-by: Yongseop Kim <[email protected]>
  • Loading branch information
YongseopKim authored Feb 28, 2024
1 parent ecd17dd commit aaaeb13
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 15 additions & 3 deletions tests/nnfw_api/lib/GenModelTrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class GenModelTrainContext : public GenModelTestContext
{
public:
GenModelTrainContext(CircleBuffers &&cbufs)
: GenModelTestContext(std::move(cbufs.circle)), _cpbuf{std::move(cbufs.circle_plus)}
: GenModelTestContext(std::move(cbufs.circle)), _cpbuf{std::move(cbufs.circle_plus)}, _epoch(0)
{
// DO NOTHING
}
Expand All @@ -105,9 +105,21 @@ class GenModelTrainContext : public GenModelTestContext
*/
void addTrainCase(const TrainCaseData &tc) { _train_cases.emplace_back(tc); }

int epoch() { return _epoch; }

void setEpoch(int32_t epoch)
{
if (epoch < 2)
{
throw std::runtime_error{"epoch should be equal or greater than 2"};
}
_epoch = epoch;
}

private:
CircleBuffer _cpbuf;
std::vector<TrainCaseData> _train_cases;
int32_t _epoch;
};

/**
Expand Down Expand Up @@ -237,8 +249,8 @@ class GenModelTrain : public ::testing::Test
// Prepare expected loss
_so.losses.resize(num_expecteds);

const int num_epoch = 1;
ASSERT_EQ(num_epoch, 1); // for now, epoch is set to 1
const int num_epoch = _context->epoch();
ASSERT_GE(num_epoch, 2);
const int num_step = num_expecteds / tri.batch_size;
ASSERT_GE(num_step, 1);

Expand Down
6 changes: 4 additions & 2 deletions tests/nnfw_api/src/GenModelTests/one_op_trains/Conv2D.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST_F(GenModelTrain, OneOp_Conv2D)
uint32_t weight_buf = cgen.addBuffer(std::vector<float>(2 * 3 * 3, 0.f));
int weight = cgen.addTensor({{2, 3, 3, 1}, circle::TensorType::TensorType_FLOAT32, weight_buf});
uint32_t bias_buf = cgen.addBuffer(std::vector<float>(2, 0.f));
int bias = cgen.addTensor({{1, 1, 1, 2}, circle::TensorType::TensorType_FLOAT32, bias_buf});
int bias = cgen.addTensor({{2}, circle::TensorType::TensorType_FLOAT32, bias_buf});
int out = cgen.addTensor({{1, 3, 3, 2}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorConv2D({{in, weight, bias}, {out}}, circle::Padding_VALID, 1, 1,
circle::ActivationFunctionType_NONE, 1, 1);
Expand All @@ -41,9 +41,11 @@ TEST_F(GenModelTrain, OneOp_Conv2D)
{{4, 0, -5, 1, 0, 4, -1, 1, -1, -3, 3, -2, -4,
1, -2, 2, 4, -4, 2, 2, 0, 4, -1, -2, 4}}, // inputs
{{47, -4, -25, 9, 10, 10, -13, 11, -14, -26, -12, 26, 20, 40, 1, 3, 11, 4}}, // expected
{{403.333f}} // loss
{{62469.609375f}} // loss
));
_context->setBackends({"train"});
// To apply backward to loss, epoch should be >= 2
_context->setEpoch(2);

SUCCEED();
}

0 comments on commit aaaeb13

Please sign in to comment.