ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Unpack.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 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 "kernels/Unpack.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
22
23#include <stdexcept>
24
25namespace luci_interpreter
26{
27
28namespace kernels
29{
30
31Unpack::Unpack(const Tensor *input, std::vector<Tensor *> outputs, const UnpackParams &params)
32 : KernelWithParams<UnpackParams>({input}, std::move(outputs), params)
33{
34}
35
37{
38 const Shape &input_shape = input()->shape();
39
40 int axis = _params.axis;
41 if (axis < 0)
42 axis += input()->shape().num_dims();
43 assert(axis >= 0 && axis < input_shape.num_dims());
44
45 Shape output_shape(input_shape.num_dims() - 1);
46 int out_index = 0;
47 for (int in_index = 0; in_index < input_shape.num_dims(); ++in_index)
48 {
49 if (in_index != axis)
50 output_shape.dim(out_index++) = input_shape.dim(in_index);
51 }
52
53 for (Tensor *output : _outputs)
54 {
55 assert(output->element_type() == input()->element_type());
57 }
58}
59
60template <typename T> void Unpack::executeImpl() const
61{
62 tflite::UnpackParams params{};
64 params.num_split = _outputs.size();
65 VectorOfTensors<T, false> all_outputs(_outputs);
66 tflite::reference_ops::Unpack<T>(params, getTensorShape(input()), getTensorData<T>(input()),
67 **all_outputs.shapes(), all_outputs.data());
68}
69
70void Unpack::execute() const
71{
72 switch (input()->element_type())
73 {
74 case DataType::FLOAT32:
75 return executeImpl<float>();
76 case DataType::U8:
77 return executeImpl<uint8_t>();
78 default:
79 throw std::runtime_error("luci-intp Unpack Unsupported type.");
80 }
81}
82
83} // namespace kernels
84} // namespace luci_interpreter
const std::vector< Tensor * > _outputs
Definition Kernel.h:53
const UnpackParams & params() const
Definition Kernel.h:67
int32_t dim(int i) const
Definition Tensor.h:41
int num_dims() const
Definition Tensor.h:39
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
DataType element_type() const
Definition Tensor.h:105
Tensor * output(int index) const
Definition Unpack.h:34
const Tensor * input() const
Definition Unpack.h:33
void execute() const override
Definition Unpack.cpp:70
Unpack(const Tensor *input, std::vector< Tensor * > outputs, const UnpackParams &params)
Definition Unpack.cpp:31
const luci_interpreter::RuntimeShape output_shape
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194