ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BinaryArithmetic.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../KernelGenerator.h"
18#include "../Validator.h"
19
21#include <AclKernelGen.h>
22
24{
25
27 acl_common::AclActivationBuilder<::arm_compute::ITensor, ::arm_compute::NEActivationLayer,
28 acl_common::AclFunction>;
29
30void Validator::visit(const ir::operation::BinaryArithmetic &) { _supported = true; }
31
32void KernelGenerator::visit(const ir::operation::BinaryArithmetic &node)
33{
34 const auto ofm_index{node.getOutputs().at(0)};
35 const auto lhs_index{node.getInputs().at(ir::operation::BinaryArithmetic::Input::LHS)};
36 const auto rhs_index{node.getInputs().at(ir::operation::BinaryArithmetic::Input::RHS)};
37
38 const auto activation = node.param().activation;
39
40 auto ofm_tensor = _tensor_reg->getAclTensor(ofm_index);
41 auto lhs_tensor = _tensor_reg->getAclTensor(lhs_index);
42 auto rhs_tensor = _tensor_reg->getAclTensor(rhs_index);
43
44 std::unique_ptr<arm_compute::IFunction> fn;
45 switch (node.param().arithmetic_type)
46 {
48 {
49 arm_compute::NEArithmeticAddition::validate(lhs_tensor->info(), rhs_tensor->info(),
50 ofm_tensor->info(),
51 arm_compute::ConvertPolicy::SATURATE)
52 .throw_if_error();
53 fn = acl_common::generateLayer<arm_compute::NEArithmeticAddition>(
54 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
55 arm_compute::ConvertPolicy::SATURATE);
56 break;
57 }
59 {
60 arm_compute::NEArithmeticSubtraction::validate(lhs_tensor->info(), rhs_tensor->info(),
61 ofm_tensor->info(),
62 arm_compute::ConvertPolicy::SATURATE)
63 .throw_if_error();
64 fn = acl_common::generateLayer<arm_compute::NEArithmeticSubtraction>(
65 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(),
66 arm_compute::ConvertPolicy::SATURATE);
67 break;
68 }
70 {
71 arm_compute::NEPixelWiseMultiplication::validate(
72 lhs_tensor->info(), rhs_tensor->info(), ofm_tensor->info(), 1.0,
73 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO)
74 .throw_if_error();
75 // RoundingPolicy for scale:1.0 is only allowed RoundingPolicy::TO_ZERO
76 fn = acl_common::generateLayer<arm_compute::NEPixelWiseMultiplication>(
77 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle(), 1.0, // scale
78 arm_compute::ConvertPolicy::SATURATE, arm_compute::RoundingPolicy::TO_ZERO);
79 break;
80 }
82 {
83 arm_compute::NEElementwiseDivision::validate(lhs_tensor->info(), rhs_tensor->info(),
84 ofm_tensor->info())
85 .throw_if_error();
86 fn = acl_common::generateLayer<arm_compute::NEElementwiseDivision>(
87 lhs_tensor->handle(), rhs_tensor->handle(), ofm_tensor->handle());
88 break;
89 }
90 default:
91 assert(false && "The BinaryArithmetic operation supports only binary arithmetic operations");
92 break;
93 }
94 _return_fn = std::make_unique<exec::FunctionSequence>(
95 acl_common::asAclFunction(std::move(fn)),
96 ActivationBuilder::generate(activation, ofm_tensor->handle()));
97}
98
99} // namespace onert::backend::acl_neon
static std::unique_ptr< exec::IFunction > generate(ir::Activation code, T_Tensor *ifm_alloc)
std::unique_ptr< exec::IFunction > _return_fn
std::unique_ptr< AclFunction > asAclFunction(std::unique_ptr<::arm_compute::IFunction > &&layer)
Definition Convert.cc:246
::onert::backend::acl_common::AclActivationBuilder< ::arm_compute::ITensor, ::arm_compute::NEActivationLayer, acl_common::AclFunction > ActivationBuilder