1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
19 #include "tensorflow/lite/kernels/kernel_util.h"
20 #include "tensorflow/lite/micro/kernels/kernel_util.h"
21 
22 namespace tflite {
23 namespace ops {
24 namespace micro {
25 namespace unpack {
26 namespace {
27 
28 constexpr int kInputTensor = 0;
29 
30 template <typename T>
UnpackImpl(TfLiteContext * context,TfLiteNode * node,const TfLiteEvalTensor * input,int output_count,int axis)31 TfLiteStatus UnpackImpl(TfLiteContext* context, TfLiteNode* node,
32                         const TfLiteEvalTensor* input, int output_count,
33                         int axis) {
34   const TfLiteEvalTensor* output0 =
35       tflite::micro::GetEvalOutput(context, node, 0);
36   const TfLiteIntArray* input_dims = input->dims;
37   const TfLiteIntArray* output_dims = output0->dims;
38   const int dimensions = input_dims->size;
39 
40   if (axis < 0) {
41     axis += input->dims->size;
42   }
43 
44   TFLITE_DCHECK_LT(axis, dimensions);
45 
46   int outer_size = 1;
47   for (int i = 0; i < axis; ++i) {
48     outer_size *= input_dims->data[i];
49   }
50   int copy_size = 1;
51   for (int i = axis + 1; i < dimensions; ++i) {
52     copy_size *= input_dims->data[i];
53   }
54   int output_size = 1;
55   for (int i = 0; i < output_dims->size; ++i) {
56     output_size *= output_dims->data[i];
57   }
58   TFLITE_DCHECK_EQ(output_size, copy_size * outer_size);
59 
60   const T* input_data = tflite::micro::GetTensorData<T>(input);
61 
62   for (int i = 0; i < output_count; ++i) {
63     TfLiteEvalTensor* t = tflite::micro::GetEvalOutput(context, node, i);
64     T* output_data = tflite::micro::GetTensorData<T>(t);
65     for (int k = 0; k < outer_size; ++k) {
66       T* output_ptr = output_data + copy_size * k;
67       int loc = k * output_count * copy_size + i * copy_size;
68       const T* input_ptr = input_data + loc;
69       for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j];
70     }
71   }
72 
73   return kTfLiteOk;
74 }
75 
Eval(TfLiteContext * context,TfLiteNode * node)76 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
77   TfLiteUnpackParams* data =
78       reinterpret_cast<TfLiteUnpackParams*>(node->builtin_data);
79 
80   const TfLiteEvalTensor* input =
81       tflite::micro::GetEvalInput(context, node, kInputTensor);
82 
83   switch (input->type) {
84     case kTfLiteFloat32: {
85       return UnpackImpl<float>(context, node, input, data->num, data->axis);
86     }
87     case kTfLiteInt32: {
88       return UnpackImpl<int32_t>(context, node, input, data->num, data->axis);
89     }
90     case kTfLiteUInt8: {
91       return UnpackImpl<uint8_t>(context, node, input, data->num, data->axis);
92     }
93     case kTfLiteInt8: {
94       return UnpackImpl<int8_t>(context, node, input, data->num, data->axis);
95     }
96     default: {
97       TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by unpack.",
98                          TfLiteTypeGetName(input->type));
99       return kTfLiteError;
100     }
101   }
102 
103   return kTfLiteOk;
104 }
105 }  // namespace
106 }  // namespace unpack
107 
Register_UNPACK()108 TfLiteRegistration Register_UNPACK() {
109   return {/*init=*/nullptr,
110           /*free=*/nullptr,
111           /*prepare=*/nullptr,
112           /*invoke=*/unpack::Eval,
113           /*profiling_string=*/nullptr,
114           /*builtin_code=*/0,
115           /*custom_name=*/nullptr,
116           /*version=*/0};
117 }
118 
119 }  // namespace micro
120 }  // namespace ops
121 }  // namespace tflite
122