ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ReduceMax.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 reduceMaxGeneric(kernels::TISOData *tiso_data, const circle::Tensor *input,
32 const circle::Tensor *axis, const circle::Tensor *output)
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=*/std::numeric_limits<T>::lowest(), output_shape.flatSize(),
46 [](const float current, const float in) -> float { return (in > current) ? in : current; });
47}
48
49} // namespace
50
51void configure_kernel_CircleReduceMax(const circle::Operator *cur_op,
52 BaseRuntimeGraph *runtime_graph)
53{
54 kernels::TISOKernel kernel(cur_op, runtime_graph);
55
56 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
57 Tensor::element_type(kernel.output()));
58 LUCI_INTERPRETER_CHECK(Tensor::num_dims(kernel.input1()) == Tensor::num_dims(kernel.output()));
59 LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input2()) == DataType::S32);
60
61 const int32_t axis_value =
62 kernels::getTensorData<int>(runtime_graph->getConstDataByTensor(kernel.input2()))[0];
63 LUCI_INTERPRETER_CHECK(axis_value >= 0);
64}
65
66void execute_kernel_CircleReduceMax(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
67{
68 kernels::TISOKernel kernel(cur_op, runtime_graph);
69 kernels::TISOData tiso_data = kernel.readData();
70
71 const auto *input = kernel.input1();
72 const auto *axis = kernel.input2();
73 const auto *output = kernel.output();
74
75 switch (Tensor::element_type(kernel.input1()))
76 {
77#ifndef DIS_FLOAT
78 case DataType::FLOAT32:
79 reduceMaxGeneric<float>(&tiso_data, input, axis, output);
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_CircleReduceMax(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition ReduceMax.cpp:51
void execute_kernel_CircleReduceMax(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition ReduceMax.cpp:66
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)