Skip to content

Commit

Permalink
Apply format.patch
Browse files Browse the repository at this point in the history
  • Loading branch information
YongseopKim committed Jan 24, 2024
1 parent bf0356c commit e8985be
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
27 changes: 12 additions & 15 deletions compute/cker/include/cker/train/operation/Pad.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace train
{

/*
* input_data will be transformed by PAD operation with padding options(such as constant C) to output_data
* input_data will be transformed by PAD operation with padding options(such as constant C) to
* output_data
*
* input_data -> output_data
* [0,1] -> [C,C,C,C]
Expand All @@ -41,11 +42,13 @@ inline void Pad(const int32_t *padding_data, int32_t pad_rank, const Shape &inpu
const T *constant_value_data)
{
// Use nnfw::cker::Pad directly
nnfw::cker::Pad<T>(padding_data, pad_rank, input_shape, input_data, output_shape, output_data, constant_value_data);
nnfw::cker::Pad<T>(padding_data, pad_rank, input_shape, input_data, output_shape, output_data,
constant_value_data);
}

/*
* input_data(backward_output_data) will be transformed by backward of PAD operation with padding options to output_data(backward_input_data)
* input_data(backward_output_data) will be transformed by backward of PAD operation with padding
* options to output_data(backward_input_data)
*
* input_data(backward_output_data) -> output_data(backward_input_data)
* [C,C,C,C] -> [0,1]
Expand Down Expand Up @@ -117,12 +120,9 @@ inline void Depad(const int32_t *padding_data, int32_t pad_rank, const Shape &in
for (auto h = 0; h < out_height; ++h)
{
// TODO: Remove unnecessary literal value 0
const auto in_offset = (d + padding_depth) * in_plain_size +
(h + padding_top) * in_width +
(0 + padding_left);
const auto out_offset = (d * out_plain_size) +
(h * out_width) +
(0);
const auto in_offset =
(d + padding_depth) * in_plain_size + (h + padding_top) * in_width + (0 + padding_left);
const auto out_offset = (d * out_plain_size) + (h * out_width) + (0);
// copy a row of input data to output data
std::memcpy(output_data + out_offset, input_data + in_offset, out_width * sizeof(T));
}
Expand Down Expand Up @@ -155,12 +155,9 @@ inline void Depad(const int32_t *padding_data, int32_t pad_rank, const Shape &in
// TODO: Remove unnecessary literal value 0
const auto in_offset = (c + padding_cube) * in_cube_size +
(d + padding_depth) * in_plain_size +
(h + padding_top) * in_width +
(0 + padding_left);
const auto out_offset = (c * out_cube_size) +
(d * out_plain_size) +
(h * out_width) +
(0);
(h + padding_top) * in_width + (0 + padding_left);
const auto out_offset =
(c * out_cube_size) + (d * out_plain_size) + (h * out_width) + (0);
// copy a row of input data to output data
std::memcpy(output_data + out_offset, input_data + in_offset, out_width * sizeof(T));
}
Expand Down
77 changes: 38 additions & 39 deletions compute/cker/src/train/Pad.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ template <typename T> class PadOpVerifier
T _constant_value;

public:
PadOpVerifier(const PadParams &op_params, const Shape &in_shape, const Shape &out_shape, T constant_value)
: _op_params(op_params), _in_shape(in_shape), _out_shape(out_shape), _constant_value(constant_value)
PadOpVerifier(const PadParams &op_params, const Shape &in_shape, const Shape &out_shape,
T constant_value)
: _op_params(op_params), _in_shape(in_shape), _out_shape(out_shape),
_constant_value(constant_value)
{
// DO NOTHING
}
Expand All @@ -48,27 +50,23 @@ template <typename T> class PadOpVerifier
assert(expected_output.size() == _out_shape.FlatSize());

std::vector<T> cacluated_output(_out_shape.FlatSize());
nnfw::cker::train::Pad(_op_params.data, _op_params.rank,
_in_shape, input.data(),
_out_shape, cacluated_output.data(),
&_constant_value);
nnfw::cker::train::Pad(_op_params.data, _op_params.rank, _in_shape, input.data(), _out_shape,
cacluated_output.data(), &_constant_value);

if (expect_eq)
EXPECT_EQ(expected_output, cacluated_output);
else
EXPECT_NE(expected_output, cacluated_output);
}


void verifyBackward(const std::vector<T> backward_output, const std::vector<T> expected_backward_input,
bool expect_eq = true)
void verifyBackward(const std::vector<T> backward_output,
const std::vector<T> expected_backward_input, bool expect_eq = true)
{
assert(backward_output.size() == _out_shape.FlatSize());
assert(expected_backward_input.size() == _in_shape.FlatSize());

std::vector<T> backward_input(_in_shape.FlatSize());
nnfw::cker::train::Depad(_op_params.data, _op_params.rank,
_out_shape, backward_output.data(),
nnfw::cker::train::Depad(_op_params.data, _op_params.rank, _out_shape, backward_output.data(),
_in_shape, backward_input.data());

if (expect_eq)
Expand Down Expand Up @@ -103,7 +101,7 @@ TEST(CKer_Operation, Pad)
PadOpVerifier<float> verifier(op_param, in, out, constant_value);

std::vector<float> input = {1.f};
std::vector<float> expected_output = {3.f,1.f,3.f};
std::vector<float> expected_output = {3.f, 1.f, 3.f};
verifier.verifyForward(input, expected_output);
verifier.verifyBackward(expected_output, input);
}
Expand Down Expand Up @@ -139,15 +137,15 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 3.f;

nnfw::cker::Shape in = {1,1};
nnfw::cker::Shape out = {3,3};
nnfw::cker::Shape in = {1, 1};
nnfw::cker::Shape out = {3, 3};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 1.f;
std::vector<float> input = {init_value};
std::vector<float> expected_output(3*3, constant_value);
expected_output[expected_output.size()/2] = init_value;
std::vector<float> expected_output(3 * 3, constant_value);
expected_output[expected_output.size() / 2] = init_value;
verifier.verifyForward(input, expected_output);
verifier.verifyBackward(expected_output, input);
}
Expand All @@ -162,19 +160,19 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 1.f;

nnfw::cker::Shape in = {3,3};
nnfw::cker::Shape out = {9,9};
nnfw::cker::Shape in = {3, 3};
nnfw::cker::Shape out = {9, 9};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 1.f;
std::vector<float> input(3*3, init_value);
std::vector<float> expected_output(9*9, constant_value);
std::vector<float> input(3 * 3, init_value);
std::vector<float> expected_output(9 * 9, constant_value);
for (auto i = -1; i <= 1; ++i)
{
for (auto j = -1; j <= 1; ++j)
{
size_t ind = (9 * i) + (expected_output.size()/2 + j);
size_t ind = (9 * i) + (expected_output.size() / 2 + j);
expected_output[ind] = init_value;
}
}
Expand All @@ -196,15 +194,15 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 3.f;

nnfw::cker::Shape in = {1,1,1};
nnfw::cker::Shape out = {3,3,3};
nnfw::cker::Shape in = {1, 1, 1};
nnfw::cker::Shape out = {3, 3, 3};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 1.f;
std::vector<float> input = {init_value};
std::vector<float> expected_output(3*3*3, constant_value);
expected_output[expected_output.size()/2] = init_value;
std::vector<float> expected_output(3 * 3 * 3, constant_value);
expected_output[expected_output.size() / 2] = init_value;
verifier.verifyForward(input, expected_output);
verifier.verifyBackward(expected_output, input);
}
Expand All @@ -221,21 +219,21 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 7.f;

nnfw::cker::Shape in = {3,3,3};
nnfw::cker::Shape out = {13,13,13};
nnfw::cker::Shape in = {3, 3, 3};
nnfw::cker::Shape out = {13, 13, 13};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 5.f;
std::vector<float> input(3*3*3, init_value);
std::vector<float> expected_output(13*13*13, constant_value);
std::vector<float> input(3 * 3 * 3, init_value);
std::vector<float> expected_output(13 * 13 * 13, constant_value);
for (auto i = -1; i <= 1; ++i)
{
for (auto j = -1; j <= 1; ++j)
{
for (auto k = -1; k <= 1; ++k)
{
size_t ind = (13 * 13 * i) + (13 * j) + (expected_output.size()/2 + k);
size_t ind = (13 * 13 * i) + (13 * j) + (expected_output.size() / 2 + k);
expected_output[ind] = init_value;
}
}
Expand All @@ -260,15 +258,15 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 3.f;

nnfw::cker::Shape in = {1,1,1,1};
nnfw::cker::Shape out = {3,3,3,3};
nnfw::cker::Shape in = {1, 1, 1, 1};
nnfw::cker::Shape out = {3, 3, 3, 3};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 1.f;
std::vector<float> input = {init_value};
std::vector<float> expected_output(3*3*3*3, constant_value);
expected_output[expected_output.size()/2] = init_value;
std::vector<float> expected_output(3 * 3 * 3 * 3, constant_value);
expected_output[expected_output.size() / 2] = init_value;
verifier.verifyForward(input, expected_output);
verifier.verifyBackward(expected_output, input);
}
Expand All @@ -287,14 +285,14 @@ TEST(CKer_Operation, Pad)
}
float constant_value = 9.f;

nnfw::cker::Shape in = {5,5,5,5};
nnfw::cker::Shape out = {19,19,19,19};
nnfw::cker::Shape in = {5, 5, 5, 5};
nnfw::cker::Shape out = {19, 19, 19, 19};

PadOpVerifier<float> verifier(op_param, in, out, constant_value);

float init_value = 2.f;
std::vector<float> input(5*5*5*5, init_value);
std::vector<float> expected_output(19*19*19*19, constant_value);
std::vector<float> input(5 * 5 * 5 * 5, init_value);
std::vector<float> expected_output(19 * 19 * 19 * 19, constant_value);
for (auto i = -2; i <= 2; ++i)
{
for (auto j = -2; j <= 2; ++j)
Expand All @@ -303,7 +301,8 @@ TEST(CKer_Operation, Pad)
{
for (auto l = -2; l <= 2; ++l)
{
size_t ind = (19 * 19 * 19 * i) + (19 * 19 * j) + (19 * k) + (expected_output.size()/2 + l);
size_t ind =
(19 * 19 * 19 * i) + (19 * 19 * j) + (19 * k) + (expected_output.size() / 2 + l);
expected_output[ind] = init_value;
}
}
Expand Down

0 comments on commit e8985be

Please sign in to comment.