-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cker/train] Introduce Pad op in train #12522
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf0356c
[cker/train] Introduce Pad op in train
YongseopKim e8985be
Apply format.patch
YongseopKim b04c37a
Remove train pad function
YongseopKim 7d498ea
Append assertion check code
YongseopKim 74c9693
Change for logical NHWC
YongseopKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef __NNFW_CKER_TRAIN_OPERATION_PAD_H__ | ||
#define __NNFW_CKER_TRAIN_OPERATION_PAD_H__ | ||
|
||
#include "cker/operation/Pad.h" | ||
|
||
namespace nnfw | ||
{ | ||
namespace cker | ||
{ | ||
namespace train | ||
{ | ||
|
||
/* | ||
* 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] | ||
* [2,3] -> [C,0,1,C] | ||
* -> [C,2,3,C] | ||
* -> [C,C,C,C] | ||
*/ | ||
/* | ||
* input_data(backward_output_data) will be transformed by backward of PAD operation (Depad) 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] | ||
* [C,0,1,C] -> [2,3] | ||
* [C,2,3,C] -> | ||
* [C,C,C,C] -> | ||
*/ | ||
template <typename T> | ||
inline void Depad(const int32_t *padding_data, int32_t pad_rank, const Shape &input_shape, | ||
const T *input_data, const Shape &output_shape, T *output_data) | ||
{ | ||
using PaddingInfo = std::pair<int32_t, int32_t>; | ||
using PaddingList = std::vector<PaddingInfo>; | ||
|
||
assert(output_shape.DimensionsCount() == input_shape.DimensionsCount()); | ||
assert(output_shape.DimensionsCount() == pad_rank); | ||
|
||
PaddingList padding_list(pad_rank); | ||
for (int32_t n = 0; n < pad_rank; ++n) | ||
{ | ||
const int32_t *from = padding_data + (n * 2); | ||
assert(from[0] >= 0 && from[1] >= 0); | ||
padding_list[n] = {from[0], from[1]}; | ||
} | ||
for (int32_t i = 0; i < pad_rank; ++i) | ||
{ | ||
assert(output_shape.Dims(i) == | ||
input_shape.Dims(i) - padding_list[i].first - padding_list[i].second); | ||
} | ||
|
||
// logical axis: row -> col -> plain -> cube | ||
switch (pad_rank) | ||
{ | ||
case 0: | ||
case 1: | ||
{ | ||
const int32_t out_row_len = output_shape.Dims(0); | ||
const int32_t padding_left = padding_list[0].first; | ||
std::memcpy(output_data, input_data + padding_left, out_row_len * sizeof(T)); | ||
break; | ||
} | ||
case 2: // HW | ||
{ | ||
const int32_t out_col_len = output_shape.Dims(0); | ||
const int32_t out_row_len = output_shape.Dims(1); | ||
const int32_t in_row_len = input_shape.Dims(1); | ||
const int32_t padding_top = padding_list[0].first; | ||
const int32_t padding_left = padding_list[1].first; | ||
for (auto i = 0; i < out_col_len; ++i) | ||
{ | ||
const auto in_offset = (i + padding_top) * in_row_len + padding_left; | ||
const auto out_offset = i * out_row_len; | ||
// copy a row of input data to output data | ||
std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T)); | ||
} | ||
break; | ||
} | ||
case 3: // HWC | ||
{ | ||
const int32_t out_plain_len = output_shape.Dims(0); | ||
const int32_t out_col_len = output_shape.Dims(1); | ||
const int32_t out_row_len = output_shape.Dims(2); | ||
const int32_t out_plain_size = out_col_len * out_row_len; | ||
const int32_t in_col_len = input_shape.Dims(1); | ||
const int32_t in_row_len = input_shape.Dims(2); | ||
const int32_t in_plain_size = in_col_len * in_row_len; | ||
const int32_t padding_depth = padding_list[0].first; | ||
const int32_t padding_top = padding_list[1].first; | ||
const int32_t padding_left = padding_list[2].first; | ||
for (auto d = 0; d < out_plain_len; ++d) | ||
{ | ||
for (auto h = 0; h < out_col_len; ++h) | ||
{ | ||
const auto in_offset = | ||
(d + padding_depth) * in_plain_size + (h + padding_top) * in_row_len + (padding_left); | ||
const auto out_offset = (d * out_plain_size) + (h * out_row_len); | ||
// copy a row of input data to output data | ||
std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T)); | ||
} | ||
} | ||
break; | ||
} | ||
case 4: // NHWC | ||
{ | ||
const int32_t out_cube_len = output_shape.Dims(0); | ||
const int32_t out_plain_len = output_shape.Dims(1); | ||
const int32_t out_col_len = output_shape.Dims(2); | ||
const int32_t out_row_len = output_shape.Dims(3); | ||
const int32_t out_plain_size = out_col_len * out_row_len; | ||
const int32_t out_cube_size = out_plain_len * out_plain_size; | ||
const int32_t in_plain_len = input_shape.Dims(1); | ||
const int32_t in_col_len = input_shape.Dims(2); | ||
const int32_t in_row_len = input_shape.Dims(3); | ||
const int32_t in_plain_size = in_col_len * in_row_len; | ||
const int32_t in_cube_size = in_plain_len * in_plain_size; | ||
const int32_t padding_cube = padding_list[0].first; | ||
const int32_t padding_depth = padding_list[1].first; | ||
const int32_t padding_top = padding_list[2].first; | ||
const int32_t padding_left = padding_list[3].first; | ||
for (auto c = 0; c < out_cube_len; ++c) | ||
{ | ||
for (auto d = 0; d < out_plain_len; ++d) | ||
{ | ||
for (auto h = 0; h < out_col_len; ++h) | ||
{ | ||
const auto in_offset = (c + padding_cube) * in_cube_size + | ||
(d + padding_depth) * in_plain_size + | ||
(h + padding_top) * in_row_len + (padding_left); | ||
const auto out_offset = (c * out_cube_size) + (d * out_plain_size) + (h * out_row_len); | ||
// copy a row of input data to output data | ||
std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T)); | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("Padding for rank > 4 NYI"); | ||
break; | ||
} | ||
} | ||
|
||
} // namespace train | ||
} // namespace cker | ||
} // namespace nnfw | ||
|
||
#endif // __NNFW_CKER_TRAIN_OPERATION_PAD_H__ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
It would be good to use this param in cpu backend too.