ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Sum.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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 "Builders.h"
18#include "kernels/Utils.h"
19#include "TISOKernel.h"
20
21#include "PALReduceCommon.h"
22
23#include <cassert>
24
25namespace luci_interpreter
26{
27namespace
28{
29
30template <typename T>
31void sumGeneric(kernels::TISOData *tiso_data, const circle::Tensor *input,
32 const circle::Tensor *axis, const circle::Tensor *output, bool keep_dims)
33{
34 const int input_rank = Tensor::num_dims(input);
35 const int num_axis = Tensor::num_elements(axis);
36
37 auto const input_dims = wrap(input->shape());
38 const auto output_shape = kernels::getTensorShape(output);
39
40 luci_interpreter_pal::ReduceGeneric<T>(
41 kernels::getTensorData<T>(tiso_data->input1_data),
42 reinterpret_cast<const int *>(input_dims.data()), input_rank,
43 kernels::getTensorData<T>(tiso_data->output_data),
44 kernels::getTensorData<int>(tiso_data->input2_data), num_axis,
45 /*init_value=*/T(0), output_shape.flatSize(),
46 [](const float current, const float in) -> float { return in + current; });
47}
48
49} // namespace
50
51void configure_kernel_CircleSum(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
52{
53 kernels::TISOKernel kernel(cur_op, runtime_graph);
54
55 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
56 Tensor::element_type(kernel.output()));
57 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input2()) == DataType::S32);
58
59 const int32_t axis_value =
60 kernels::getTensorData<int>(runtime_graph->getConstDataByTensor(kernel.input2()))[0];
61 LUCI_INTERPRETER_CHECK(axis_value >= 0);
62}
63
64void execute_kernel_CircleSum(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
65{
66 kernels::TISOKernel kernel(cur_op, runtime_graph);
67 kernels::TISOData tiso_data = kernel.readData();
68
69 const auto *input = kernel.input1();
70 const auto *axis = kernel.input2();
71 const auto *output = kernel.output();
72
73 const auto *options = cur_op->builtin_options_as_ReducerOptions();
74
75 switch (Tensor::element_type(kernel.input1()))
76 {
77#ifndef DIS_FLOAT
78 case DataType::FLOAT32:
79 sumGeneric<float>(&tiso_data, input, axis, output, options->keep_dims());
80 break;
81#endif // DIS_FLOAT
82 default:
83 assert(false && "Unsupported type");
84 }
85}
86
87} // namespace luci_interpreter
uint8_t * getConstDataByTensor(const circle::Tensor *raw_tensor)
const circle::Tensor * output() const
Definition TISOKernel.h:62
const circle::Tensor * input2() const
Definition TISOKernel.h:61
const circle::Tensor * input1() const
Definition TISOKernel.h:60
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
void configure_kernel_CircleSum(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Sum.cpp:51
void execute_kernel_CircleSum(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Sum.cpp:64
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)