Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2018 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/c_api_internal.h"
     18 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
     19 #include "tensorflow/lite/kernels/internal/tensor.h"
     20 #include "tensorflow/lite/kernels/kernel_util.h"
     21 
     22 namespace tflite {
     23 namespace ops {
     24 namespace builtin {
     25 namespace unpack {
     26 namespace {
     27 
     28 constexpr int kInputTensor = 0;
     29 
     30 // Op data for unpack op.
     31 struct OpData {
     32   int num;
     33   int axis;
     34 };
     35 
     36 void* Init(TfLiteContext* context, const char* buffer, size_t length) {
     37   auto* data = new OpData;
     38   data->axis = 0;
     39   return data;
     40 }
     41 
     42 void Free(TfLiteContext* context, void* buffer) {
     43   delete reinterpret_cast<OpData*>(buffer);
     44 }
     45 
     46 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
     47   const OpData* data = reinterpret_cast<OpData*>(node->builtin_data);
     48 
     49   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
     50   TF_LITE_ENSURE_EQ(context, NumOutputs(node), data->num);
     51 
     52   const TfLiteTensor* input = GetInput(context, node, kInputTensor);
     53   TF_LITE_ENSURE(context, NumDimensions(input) <= 4);
     54   TF_LITE_ENSURE(context, NumDimensions(input) > 1);
     55   int axis = data->axis;
     56   if (axis < 0) {
     57     axis += NumDimensions(input);
     58   }
     59   TF_LITE_ENSURE(context, 0 <= axis && axis < NumDimensions(input));
     60   if (input->type != kTfLiteInt32 && input->type != kTfLiteFloat32) {
     61     context->ReportError(context,
     62                          "Currently pack only supports int32 and float32.");
     63     return kTfLiteError;
     64   }
     65 
     66   const TfLiteIntArray* input_shape = input->dims;
     67   // Num should be equal to the shape[axis].
     68   // Resize outputs. rank will be R - 1.
     69   TfLiteIntArray* output_shape = TfLiteIntArrayCreate(NumDimensions(input) - 1);
     70   int o = 0;
     71   for (int index = 0; index < NumDimensions(input); ++index) {
     72     if (index != axis) {
     73       output_shape->data[o++] = input_shape->data[index];
     74     }
     75   }
     76 
     77   TF_LITE_ENSURE_EQ(context, data->num, input_shape->data[axis]);
     78   for (int i = 0; i < data->num; ++i) {
     79     TfLiteIntArray* copied_output_shape = TfLiteIntArrayCopy(output_shape);
     80     TfLiteTensor* output = GetOutput(context, node, i);
     81     TF_LITE_ENSURE_EQ(context, output->type, input->type);
     82     TF_LITE_ENSURE_OK(
     83         context, context->ResizeTensor(context, output, copied_output_shape));
     84   }
     85 
     86   TfLiteIntArrayFree(output_shape);
     87   return kTfLiteOk;
     88 }
     89 
     90 template <typename T>
     91 void UnpackImpl(TfLiteContext* context, TfLiteNode* node,
     92                 const TfLiteTensor* input, int output_count, int axis) {
     93   tflite::UnpackParams op_params;
     94   op_params.axis = axis;
     95   op_params.num_split = output_count;
     96   VectorOfTensors<T> all_outputs(*context, *node->outputs);
     97   reference_ops::Unpack<T>(op_params, GetTensorShape(input),
     98                            GetTensorData<T>(input), **all_outputs.shapes(),
     99                            all_outputs.data());
    100 }
    101 
    102 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
    103   const OpData* data = reinterpret_cast<OpData*>(node->builtin_data);
    104 
    105   const TfLiteTensor* input = GetInput(context, node, kInputTensor);
    106   switch (input->type) {
    107     case kTfLiteFloat32: {
    108       UnpackImpl<float>(context, node, input, data->num, data->axis);
    109       break;
    110     }
    111     case kTfLiteInt32: {
    112       UnpackImpl<int32_t>(context, node, input, data->num, data->axis);
    113       break;
    114     }
    115     default: {
    116       context->ReportError(context,
    117                            "Currently pack only supports int32 and float32.");
    118       return kTfLiteError;
    119     }
    120   }
    121 
    122   return kTfLiteOk;
    123 }
    124 }  // namespace
    125 }  // namespace unpack
    126 
    127 TfLiteRegistration* Register_UNPACK() {
    128   static TfLiteRegistration r = {unpack::Init, unpack::Free, unpack::Prepare,
    129                                  unpack::Eval};
    130   return &r;
    131 }
    132 
    133 }  // namespace builtin
    134 }  // namespace ops
    135 }  // namespace tflite
    136