Skip to content
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

[onert/train] Add PadLayer op to train backend #12535

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/onert/backend/cpu/ops/PadLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PadLayer : public ::onert::exec::IFunction

void run() override;

private:
protected:
const IPortableTensor *_input;
IPortableTensor *_output;

Expand Down
1 change: 1 addition & 0 deletions runtime/onert/backend/train/ops/OperationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using cpu::ops::getShape;
using cpu::ops::getNumberOfDimensions;
using cpu::ops::getNumberOfElements;
using cpu::ops::getSizeOfDimension;
using cpu::ops::ConstDataPtr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. You're right. I'll fix it.


/**
* @brief backpropagate acitvation
Expand Down
74 changes: 74 additions & 0 deletions runtime/onert/backend/train/ops/PadLayer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.
*/

#include "PadLayer.h"

#include <cker/train/operation/Pad.h>

namespace onert
{
namespace backend
{
namespace train
{
namespace ops
{

PadLayer::PadLayer() : cpu::ops::PadLayer(), _back_prop_input{nullptr}, _back_prop_output{nullptr}
{
// DO NOTHING
}

template <typename T> void PadLayer::depad()
{
nnfw::cker::train::Depad<T>(_padData, _padRank, getShape(_back_prop_output),
getBuffer<T>(_back_prop_output), getShape(_back_prop_input),
getBuffer<T>(_back_prop_input));
}

void PadLayer::configure(const IPortableTensor *input, IPortableTensor *output,
const int32_t *padData, int32_t padRank, const void *constantValueData,
IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output)
{
cpu::ops::PadLayer::configure(input, output, padData, padRank, constantValueData);
_back_prop_input = back_prop_input;
_back_prop_output = back_prop_output;
}

void PadLayer::forward(bool) { cpu::ops::PadLayer::run(); }

void PadLayer::backward()
{
switch (_back_prop_output->data_type())
{
case OperandType::FLOAT32:
depad<float>();
break;
case OperandType::QUANT_UINT8_ASYMM:
depad<uint8_t>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

break;
case OperandType::QUANT_INT8_ASYMM:
depad<int8_t>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto. 👍

break;
default:
throw std::runtime_error{"Pad: unsupported data type"};
}
}

} // namespace ops
} // namespace train
} // namespace backend
} // namespace onert
61 changes: 61 additions & 0 deletions runtime/onert/backend/train/ops/PadLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__
#define __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__

#include <ops/PadLayer.h>
#include <backend/IPortableTensor.h>
#include "OperationUtils.h"

#include <exec/train/ITrainableFunction.h>

namespace onert
{
namespace backend
{
namespace train
{
namespace ops
{

// Note, this is pad with mode=`CONSTANT`: it doesn't support `REFLECT` and
// `SYMMETRIC`
class PadLayer : public ::onert::exec::train::ITrainableFunction, public cpu::ops::PadLayer
{
public:
PadLayer();

public:
template <typename T> void depad();

void configure(const IPortableTensor *input, IPortableTensor *output, const int32_t *padData,
int32_t padRank, const void *constantValueData, IPortableTensor *back_prop_input,
const IPortableTensor *back_prop_output);
void forward(bool training) override;
void backward() override;

private:
IPortableTensor *_back_prop_input;
const IPortableTensor *_back_prop_output;
};

} // namespace ops
} // namespace train
} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__
Loading