ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Squeeze.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 "SISOKernel.h"
20
21#include <cassert>
22
23namespace luci_interpreter
24{
25
26void configure_kernel_CircleSqueeze(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
27{
28 kernels::SISOKernel kernel(cur_op, runtime_graph);
29
30 assert(cur_op->inputs()->size() == 1);
31
32 const circle::Tensor *input = kernel.input();
33 const circle::Tensor *output = kernel.output();
34
35 assert(Tensor::num_elements(input) == Tensor::num_elements(output));
36
37 const uint8_t *input_data = runtime_graph->getDataByTensor(input);
38 uint8_t *output_data = runtime_graph->getDataByTensor(output);
39
40 int input_num_dims = kernels::getTensorShape(input).dimensionsCount();
41
42 // Get parameters
43 const circle::SqueezeOptions *op_params = cur_op->builtin_options_as_SqueezeOptions();
44
45 // Verification of the Squeeze parameters
46 int num_squeeze_dims = op_params->squeeze_dims()->size();
47 assert(input_num_dims <= 8);
48 bool should_squeeze[8] = {false};
49 int num_squeezed_dims = 0;
50 if (num_squeeze_dims == 0)
51 {
52 for (int idx = 0; idx < input_num_dims; ++idx)
53 {
54
55 if (kernels::getTensorShape(input).dims(idx) == 1)
56 {
57 should_squeeze[idx] = true;
58 ++num_squeezed_dims;
59 }
60 }
61 }
62 else
63 {
64 for (int idx = 0; idx < num_squeeze_dims; ++idx)
65 {
66 int current = (*op_params->squeeze_dims())[idx] < 0
67 ? (*op_params->squeeze_dims())[idx] + input_num_dims
68 : (*op_params->squeeze_dims())[idx];
69 assert(current >= 0 && current < input_num_dims &&
70 kernels::getTensorShape(input).dims(current) == 1);
71 if (!should_squeeze[current])
72 ++num_squeezed_dims;
73 should_squeeze[current] = true;
74 }
75 }
76}
77
78void execute_kernel_CircleSqueeze(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
79{
80 kernels::SISOKernel kernel(cur_op, runtime_graph);
81
82 assert(cur_op->inputs()->size() == 1);
83
84 const circle::Tensor *input = kernel.input();
85 const circle::Tensor *output = kernel.output();
86
87 const uint8_t *input_data = runtime_graph->getDataByTensor(input);
88 uint8_t *output_data = runtime_graph->getDataByTensor(output);
89
90 assert(input_data != nullptr);
91 assert(output_data != nullptr);
92
93 assert(Tensor::num_elements(input) == Tensor::num_elements(output));
94
95 std::memcpy(output_data, input_data,
96 getDataTypeSize(Tensor::element_type(input)) * Tensor::num_elements(input));
97}
98
99} // namespace luci_interpreter
uint8_t * getDataByTensor(const circle::Tensor *raw_tensor)
const circle::Tensor * output() const
Definition SISOKernel.h:47
const circle::Tensor * input() const
Definition SISOKernel.h:46
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
void execute_kernel_CircleSqueeze(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Squeeze.cpp:78
void configure_kernel_CircleSqueeze(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
Definition Squeeze.cpp:26