ONE - On-device Neural Engine
Loading...
Searching...
No Matches
SquaredDifference.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMRuntimeShape.h"
21
22#include "execute/OMUtils.h"
25
26#include "PALSquaredDifference.h"
27
28using namespace onert_micro;
29using namespace onert_micro::execute;
30
31namespace
32{
33
34constexpr uint32_t numInput = 2;
35constexpr uint32_t numOutput = 1;
36
37constexpr uint32_t input1TensorIdx = 0;
38constexpr uint32_t input2TensorIdx = 1;
39constexpr uint32_t outputTensorIdx = 0;
40
41} // namespace
42
43// NOTE: doesnt currently support dynamic shapes
44// TODO: reduce code duplication with Add, Mul
46onert_micro::execute::execute_kernel_CircleSquaredDifference(const OMExecuteArgs &execute_args)
47{
48 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
49 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
50 uint16_t op_index = execute_args.kernel_index;
51
52 const circle::Tensor *input1;
53 const circle::Tensor *input2;
54 const circle::Tensor *output;
55
56 uint8_t *input1_data;
57 uint8_t *input2_data;
58 uint8_t *output_data;
59
60 // Read kernel
61 {
62 execute::OMRuntimeKernel runtime_kernel;
63 runtime_kernel.readKernel(op_index, runtime_context);
64
65 input1 = runtime_kernel.inputs[input1TensorIdx];
66 input2 = runtime_kernel.inputs[input2TensorIdx];
67 output = runtime_kernel.outputs[outputTensorIdx];
68 assert(input1 != nullptr);
69 assert(input2 != nullptr);
70 assert(output != nullptr);
71
72 runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
73
74 input1_data = runtime_kernel.inputs_data[input1TensorIdx];
75 input2_data = runtime_kernel.inputs_data[input2TensorIdx];
77 assert(input1_data != nullptr);
78 assert(input2_data != nullptr);
79 assert(output_data != nullptr);
80 }
81
82 OMStatus status;
83
84 core::OMRuntimeShape input1_shape(input1);
85 core::OMRuntimeShape input2_shape(input2);
87
89 const bool need_broadcast = pal::processBroadcastShapes(input1_shape, input2_shape, &params);
90
91 switch (input1->type())
92 {
93#ifndef DIS_FLOAT
94 case circle::TensorType_FLOAT32:
95 {
97 circle::ActivationFunctionType::ActivationFunctionType_NONE, &params.float_activation_min,
98 &params.float_activation_max);
99 if (need_broadcast)
100 {
102 params, input1_shape, core::utils::castInputData<float>(input1_data), input2_shape,
103 core::utils::castInputData<float>(input2_data), output_shape,
104 core::utils::castOutputData<float>(output_data));
105 }
106 else
107 {
108 status = pal::SquaredDifference(params, input1_shape.flatSize(),
109 core::utils::castInputData<float>(input1_data),
110 core::utils::castInputData<float>(input2_data),
111 core::utils::castOutputData<float>(output_data));
112 }
113 }
114 break;
115#endif // DIS_FLOAT
116 default:
117 {
118 status = UnsupportedType;
119 assert(false && "Unsupported type.");
120 }
121 }
122
123 return status;
124}
uint8_t * outputs_data[maxOutputSize]
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t input1TensorIdx
constexpr uint32_t outputTensorIdx
constexpr uint32_t input2TensorIdx
OMStatus BroadcastSquaredDifference4DSlow(const core::BinaryArithmeticBroadcastParams &params, const core::OMRuntimeShape &input1_shape, const T *input1_data, const core::OMRuntimeShape &input2_shape, const T *input2_data, const core::OMRuntimeShape &output_shape, T *output_data)
bool processBroadcastShapes(const core::OMRuntimeShape &shape0, const core::OMRuntimeShape &shape1, core::BinaryArithmeticBroadcastParams *params)
OMStatus SquaredDifference(const core::BinaryArithmeticBroadcastParams &params, const int flat_size, const T *input1_data, const T *input2_data, T *output_data)
OMStatus calculateActivationRange(circle::ActivationFunctionType activation, T *activation_min, T *activation_max)
Definition OMUtils.h:36
@ UnsupportedType
Definition OMStatus.h:26
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage