• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 #include <stdint.h>
16 #include <string.h>
17 
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
20 #include "tensorflow/lite/kernels/internal/tensor.h"
21 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
22 #include "tensorflow/lite/kernels/kernel_util.h"
23 
24 namespace tflite {
25 namespace ops {
26 namespace builtin {
27 namespace expand_dims {
28 
29 // Input indices
30 enum { kInput = 0, kAxis };
31 
32 namespace {
ExpandTensorDim(TfLiteContext * context,const TfLiteTensor & input,int axis,TfLiteTensor * output)33 TfLiteStatus ExpandTensorDim(TfLiteContext* context, const TfLiteTensor& input,
34                              int axis, TfLiteTensor* output) {
35   const TfLiteIntArray& input_dims = *input.dims;
36   if (axis < 0) {
37     axis = input_dims.size + 1 + axis;
38   }
39   TF_LITE_ENSURE(context, axis <= input_dims.size);
40 
41   TfLiteIntArray* output_dims = TfLiteIntArrayCreate(input_dims.size + 1);
42   for (int i = 0; i < output_dims->size; ++i) {
43     if (i < axis) {
44       output_dims->data[i] = input_dims.data[i];
45     } else if (i == axis) {
46       output_dims->data[i] = 1;
47     } else {
48       output_dims->data[i] = input_dims.data[i - 1];
49     }
50   }
51 
52   return context->ResizeTensor(context, output, output_dims);
53 }
54 
GetAxisValueFromTensor(TfLiteContext * context,const TfLiteTensor & axis,int * axis_value)55 TfLiteStatus GetAxisValueFromTensor(TfLiteContext* context,
56                                     const TfLiteTensor& axis, int* axis_value) {
57   TF_LITE_ENSURE_EQ(context, NumElements(&axis), 1);
58   switch (axis.type) {
59     case kTfLiteInt32:
60       *axis_value = *GetTensorData<int32_t>(&axis);
61       return kTfLiteOk;
62     case kTfLiteInt64:
63       *axis_value = *GetTensorData<int64_t>(&axis);
64       return kTfLiteOk;
65     default:
66       return kTfLiteError;
67   }
68 }
69 
70 }  // namespace
71 
Prepare(TfLiteContext * context,TfLiteNode * node)72 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
73   TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
74   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
75   const TfLiteTensor* input;
76   TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInput, &input));
77   const TfLiteTensor* axis;
78   TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kAxis, &axis));
79   TfLiteTensor* output;
80   TF_LITE_ENSURE_OK(context, GetOutputSafe(context, node, 0, &output));
81   output->type = input->type;
82   if (IsConstantTensor(axis)) {
83     int axis_value;
84     TF_LITE_ENSURE_OK(context,
85                       GetAxisValueFromTensor(context, *axis, &axis_value));
86     return ExpandTensorDim(context, *input, axis_value, output);
87   }
88   SetTensorToDynamic(output);
89   return kTfLiteOk;
90 }
91 
Eval(TfLiteContext * context,TfLiteNode * node)92 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
93   // Just copy input to output.
94   const TfLiteTensor* input;
95   TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInput, &input));
96   TfLiteTensor* output;
97   TF_LITE_ENSURE_OK(context, GetOutputSafe(context, node, 0, &output));
98   const TfLiteTensor* axis;
99   TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kAxis, &axis));
100   if (IsDynamicTensor(output)) {
101     int axis_value;
102     TF_LITE_ENSURE_OK(context,
103                       GetAxisValueFromTensor(context, *axis, &axis_value));
104     TF_LITE_ENSURE_OK(context,
105                       ExpandTensorDim(context, *input, axis_value, output));
106   }
107   if (output->type == kTfLiteString) {
108     TfLiteTensorRealloc(input->bytes, output);
109   }
110   memcpy(output->data.raw, input->data.raw, input->bytes);
111   return kTfLiteOk;
112 }
113 
114 }  // namespace expand_dims
Register_EXPAND_DIMS()115 TfLiteRegistration* Register_EXPAND_DIMS() {
116   static TfLiteRegistration r = {nullptr, nullptr, expand_dims::Prepare,
117                                  expand_dims::Eval};
118   return &r;
119 }
120 }  // namespace builtin
121 }  // namespace ops
122 }  // namespace tflite
123