-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
. Signed-off-by: YongHyun An <yonghyunz.an@samsung.com>
- Loading branch information
YongHyun An
committed
Jan 26, 2024
1 parent
d8d8837
commit ccb639d
Showing
6 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
compute/cker/include/cker/train/operation/BinaryArithmetic.h
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,87 @@ | ||
/* | ||
* 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_BINARYARITHMETIC_H__ | ||
#define __NNFW_CKER_TRAIN_OPERATION_BINARYARITHMETIC_H__ | ||
|
||
#include "cker/Shape.h" | ||
#include "cker/eigen/Utils.h" | ||
#include "cker/operation/BroadcastTo.h" | ||
|
||
namespace nnfw | ||
{ | ||
namespace cker | ||
{ | ||
namespace train | ||
{ | ||
enum class ArithmeticType | ||
{ | ||
kAdd, | ||
kSub, | ||
kMul, | ||
kDiv, | ||
}; | ||
|
||
template <typename T> | ||
void BinaryArithmeticGrad(const Shape &lhs_shape, const T *lhs_data, const Shape &rhs_shape, | ||
const T *rhs_data, const Shape &incoming_shape, const T *incoming_data, | ||
const Shape &lhs_grad_shape, T *lhs_grad_data, | ||
const Shape &rhs_grad_shape, T *rhs_grad_data, | ||
ArithmeticType arithmetic_type) | ||
{ | ||
switch (arithmetic_type) | ||
{ | ||
case ArithmeticType::kAdd: | ||
{ | ||
BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), lhs_grad_shape, lhs_grad_data); | ||
BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), rhs_grad_shape, rhs_grad_data); | ||
} | ||
break; | ||
|
||
case ArithmeticType::kSub: | ||
{ | ||
BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), lhs_grad_shape, lhs_grad_data); | ||
|
||
auto const in_map = MapAsMatrixWithLastDimAsRows(incoming_data, incoming_shape); | ||
auto rhs_grad_map = MapAsMatrixWithLastDimAsRows(rhs_grad_data, rhs_grad_shape); | ||
rhs_grad_map = -in_map; | ||
} | ||
break; | ||
|
||
case ArithmeticType::kMul: | ||
{ | ||
auto const in_map = MapAsMatrixWithLastDimAsRows(incoming_data, incoming_shape); | ||
auto const lhs_map = MapAsMatrixWithLastDimAsRows(lhs_data, lhs_shape); | ||
auto const rhs_map = MapAsMatrixWithLastDimAsRows(rhs_data, rhs_shape); | ||
auto lhs_grad_map = MapAsMatrixWithLastDimAsRows(lhs_grad_data, lhs_grad_shape); | ||
auto rhs_grad_map = MapAsMatrixWithLastDimAsRows(rhs_grad_data, rhs_grad_shape); | ||
|
||
lhs_grad_map = rhs_map.cwiseProduct(in_map); | ||
rhs_grad_map = lhs_map.cwiseProduct(in_map); | ||
} | ||
break; | ||
|
||
case ArithmeticType::kDiv: | ||
default: | ||
throw std::runtime_error{"Unsupported Binary Arithmetic Operation"}; | ||
} | ||
} | ||
|
||
} // namespace train | ||
} // namespace cker | ||
} // namespace nnfw | ||
|
||
#endif // __NNFW_CKER_TRAIN_OPERATION_BINARYARITHMETIC_H__ |
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
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,93 @@ | ||
/* | ||
* 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 "BinaryArithmeticLayer.h" | ||
|
||
#include "OperationUtils.h" | ||
|
||
#include <cker/Shape.h> | ||
#include <cker/train/operation/BinaryArithmetic.h> | ||
#include <cker/operation/BinaryArithmeticOps.h> | ||
#include <cker/train/operation/BinaryArithmetic.h> | ||
#include <cker/train/operation/ReLU.h> | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace train | ||
{ | ||
namespace ops | ||
{ | ||
|
||
BinaryArithmeticLayer::BinaryArithmeticLayer() | ||
: cpu::ops::BinaryArithmeticLayer(), _back_prop_lhs{nullptr}, _back_prop_rhs{nullptr}, | ||
_back_prop_output{nullptr} | ||
{ | ||
// DO NOTHING | ||
} | ||
|
||
void BinaryArithmeticLayer::configure(const IPortableTensor *lhs, const IPortableTensor *rhs, | ||
IPortableTensor *output, IPortableTensor *back_prop_lhs, | ||
IPortableTensor *back_prop_rhs, | ||
const IPortableTensor *back_prop_output, | ||
const ir::Activation activation, | ||
const ArithmeticType arithmetic_type) | ||
{ | ||
if (arithmetic_type != ArithmeticType::kAdd && arithmetic_type != ArithmeticType::kSub && | ||
arithmetic_type != ArithmeticType::kMul) | ||
{ | ||
throw std::runtime_error{"Unsupported binary operation"}; | ||
} | ||
|
||
cpu::ops::BinaryArithmeticLayer::configure( | ||
lhs, rhs, output, activation, static_cast<cpu::ops::ArithmeticType>(arithmetic_type)); | ||
|
||
_back_prop_lhs = back_prop_lhs; | ||
_back_prop_rhs = back_prop_rhs; | ||
_back_prop_output = back_prop_output; | ||
_arithmetic_type = arithmetic_type; | ||
_activation = activation; | ||
} | ||
|
||
void BinaryArithmeticLayer::forward(bool) { cpu::ops::BinaryArithmeticLayer::run(); } | ||
|
||
void BinaryArithmeticLayer::backward() | ||
{ | ||
// Calculate gradient for activation | ||
const IPortableTensor *backprop_act; | ||
try | ||
{ | ||
backprop_act = | ||
backpropActivation(_activation, _output, _back_prop_output, _act_back_prop_output.get()); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
throw std::runtime_error{"train BinaryArithmeticLayer: " + std::string(e.what())}; | ||
} | ||
assert(backprop_act != nullptr); | ||
|
||
nnfw::cker::train::BinaryArithmeticGrad( | ||
getShape(_lhs), getBuffer<float>(_lhs), getShape(_rhs), getBuffer<float>(_rhs), | ||
getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_back_prop_lhs), | ||
getBuffer<float>(_back_prop_lhs), getShape(_back_prop_rhs), getBuffer<float>(_back_prop_rhs), | ||
static_cast<nnfw::cker::train::ArithmeticType>(_arithmetic_type)); | ||
} | ||
|
||
} // namespace ops | ||
} // namespace train | ||
} // namespace backend | ||
} // namespace onert |
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,72 @@ | ||
/* | ||
* 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_BINARYARITHMETICLAYER_H__ | ||
#define __ONERT_BACKEND_TRAIN_OPS_BINARYARITHMETICLAYER_H__ | ||
|
||
#include <ops/BinaryArithmeticLayer.h> | ||
#include <backend/IPortableTensor.h> | ||
|
||
#include "../Tensor.h" | ||
#include <exec/train/ITrainableFunction.h> | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace train | ||
{ | ||
namespace ops | ||
{ | ||
|
||
enum class ArithmeticType | ||
{ | ||
kAdd, | ||
kSub, | ||
kMul, | ||
kDiv, | ||
}; | ||
|
||
class BinaryArithmeticLayer : public ::onert::exec::train::ITrainableFunction, | ||
public cpu::ops::BinaryArithmeticLayer | ||
{ | ||
public: | ||
BinaryArithmeticLayer(); | ||
|
||
public: | ||
void configure(const IPortableTensor *lhs, const IPortableTensor *rhs, IPortableTensor *output, | ||
IPortableTensor *back_prop_lhs, IPortableTensor *back_prop_rhs, | ||
const IPortableTensor *back_prop_output, const ir::Activation activation, | ||
const ArithmeticType arithmetic_type); | ||
void forward(bool training) override; | ||
void backward() override; | ||
|
||
private: | ||
IPortableTensor *_back_prop_lhs; | ||
IPortableTensor *_back_prop_rhs; | ||
const IPortableTensor *_back_prop_output; | ||
|
||
ArithmeticType _arithmetic_type; | ||
ir::Activation _activation; | ||
std::unique_ptr<BackPropTensor> _act_back_prop_output; | ||
}; | ||
|
||
} // namespace ops | ||
} // namespace train | ||
} // namespace backend | ||
} // namespace onert | ||
|
||
#endif // __ONERT_BACKEND_TRAIN_OPS_BINARYARITHMETICLAYER_H__ |