2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Unpack.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
23 namespace luci_interpreter
29 Unpack::Unpack(const Tensor *input, std::vector<Tensor *> outputs, const UnpackParams ¶ms)
30 : KernelWithParams<UnpackParams>({input}, std::move(outputs), params)
34 void Unpack::configure()
36 const Shape &input_shape = input()->shape();
38 int axis = _params.axis;
40 axis += input()->shape().num_dims();
41 assert(axis >= 0 && axis < input_shape.num_dims());
43 Shape output_shape(input_shape.num_dims() - 1);
45 for (int in_index = 0; in_index < input_shape.num_dims(); ++in_index)
48 output_shape.dim(out_index++) = input_shape.dim(in_index);
51 // TODO: enable it only if kernel with dynamic shapes
52 for (Tensor *output : _outputs)
54 assert(output->element_type() == input()->element_type());
55 output->resize(output_shape);
59 template <typename T> void Unpack::executeImpl() const
61 tflite::UnpackParams params{};
62 params.axis = _params.axis;
63 params.num_split = _outputs.size();
64 VectorOfTensors<T, false> all_outputs(_outputs);
65 tflite::reference_ops::Unpack<T>(params, getTensorShape(input()), getTensorData<T>(input()),
66 **all_outputs.shapes(), all_outputs.data());
69 void Unpack::execute() const
71 switch (input()->element_type())
73 case DataType::FLOAT32:
74 return executeImpl<float>();
76 return executeImpl<uint8_t>();
78 assert(false && "Unsupported type.");
82 } // namespace kernels
83 } // namespace luci_interpreter