• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/micro/kernels/kernel_util.h"
20 
21 namespace tflite {
22 namespace ops {
23 namespace micro {
24 namespace pack {
25 namespace {
26 
27 constexpr int kOutputTensor = 0;
28 
29 template <typename T>
PackImpl(TfLiteContext * context,TfLiteNode * node,TfLiteEvalTensor * output,int values_count,int axis)30 TfLiteStatus PackImpl(TfLiteContext* context, TfLiteNode* node,
31                       TfLiteEvalTensor* output, int values_count, int axis) {
32   const TfLiteEvalTensor* input0 =
33       tflite::micro::GetEvalInput(context, node, 0);
34 
35   const int dimensions = output->dims->size;
36   const TfLiteIntArray* input_dims = input0->dims;
37   const TfLiteIntArray* output_dims = output->dims;
38 
39   if (axis < 0) {
40     axis += dimensions;
41   }
42 
43   int outer_size = 1;
44   for (int i = 0; i < axis; ++i) {
45     outer_size *= output_dims->data[i];
46   }
47   int copy_size = 1;
48   for (int i = axis + 1; i < dimensions; ++i) {
49     copy_size *= output_dims->data[i];
50   }
51   int input_size = 1;
52   for (int i = 0; i < input_dims->size; ++i) {
53     input_size *= input_dims->data[i];
54   }
55   TFLITE_DCHECK_EQ(input_size, copy_size * outer_size);
56 
57   T* output_data = tflite::micro::GetTensorData<T>(output);
58 
59   for (int i = 0; i < values_count; ++i) {
60     const TfLiteEvalTensor* t = tflite::micro::GetEvalInput(context, node, i);
61     const T* input_data = tflite::micro::GetTensorData<T>(t);
62     for (int k = 0; k < outer_size; ++k) {
63       const T* input_ptr = input_data + copy_size * k;
64       int loc = k * values_count * copy_size + i * copy_size;
65       T* output_ptr = output_data + loc;
66       for (int j = 0; j < copy_size; ++j) output_ptr[j] = input_ptr[j];
67     }
68   }
69 
70   return kTfLiteOk;
71 }
72 
Eval(TfLiteContext * context,TfLiteNode * node)73 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
74   const TfLitePackParams* data =
75       reinterpret_cast<TfLitePackParams*>(node->builtin_data);
76 
77   TfLiteEvalTensor* output =
78       tflite::micro::GetEvalOutput(context, node, kOutputTensor);
79 
80   switch (output->type) {
81     case kTfLiteFloat32: {
82       return PackImpl<float>(context, node, output, data->values_count,
83                              data->axis);
84     }
85     case kTfLiteUInt8: {
86       return PackImpl<uint8_t>(context, node, output, data->values_count,
87                                data->axis);
88     }
89     case kTfLiteInt8: {
90       return PackImpl<int8_t>(context, node, output, data->values_count,
91                               data->axis);
92     }
93     case kTfLiteInt32: {
94       return PackImpl<int32_t>(context, node, output, data->values_count,
95                                data->axis);
96     }
97     case kTfLiteInt64: {
98       return PackImpl<int64_t>(context, node, output, data->values_count,
99                                data->axis);
100     }
101     default: {
102       TF_LITE_KERNEL_LOG(context, "Type '%s' is not supported by pack.",
103                          TfLiteTypeGetName(output->type));
104       return kTfLiteError;
105     }
106   }
107 
108   return kTfLiteOk;
109 }
110 
111 }  // namespace
112 }  // namespace pack
113 
Register_PACK()114 TfLiteRegistration Register_PACK() {
115   return {/*init=*/nullptr,
116           /*free=*/nullptr,
117           /*prepare=*/nullptr,
118           /*invoke=*/pack::Eval,
119           /*profiling_string=*/nullptr,
120           /*builtin_code=*/0,
121           /*custom_name=*/nullptr,
122           /*version=*/0};
123 }
124 
125 }  // namespace micro
126 }  // namespace ops
127 }  // namespace tflite
128