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 #include <string.h>
     16 #include <vector>
     17 #include "tensorflow/contrib/lite/builtin_op_data.h"
     18 #include "tensorflow/contrib/lite/context.h"
     19 #include "tensorflow/contrib/lite/kernels/internal/tensor.h"
     20 #include "tensorflow/contrib/lite/kernels/kernel_util.h"
     21 #include "tensorflow/contrib/lite/kernels/op_macros.h"
     22 
     23 namespace tflite {
     24 namespace ops {
     25 namespace builtin {
     26 namespace squeeze {
     27 
     28 struct SqueezeContext {
     29   SqueezeContext(TfLiteContext* context, TfLiteNode* node) {
     30     params = reinterpret_cast<TfLiteSqueezeParams*>(node->builtin_data);
     31     input = GetInput(context, node, 0);
     32     output = GetOutput(context, node, 0);
     33   }
     34   TfLiteSqueezeParams* params;
     35   TfLiteTensor* input;
     36   TfLiteTensor* output;
     37 };
     38 
     39 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
     40   TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
     41   TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
     42 
     43   SqueezeContext op_context(context, node);
     44   int input_num_dims = NumDimensions(op_context.input);
     45   int num_squeeze_dims = op_context.params->num_squeeze_dims;
     46 
     47   // Determines number of dimensions of output tensor after squeeze.
     48   const TfLiteIntArray* input_dims = op_context.input->dims;
     49   const int* squeeze_dims = op_context.params->squeeze_dims;
     50   TF_LITE_ENSURE(context, input_num_dims <= 8);
     51   bool should_squeeze[8] = {false};
     52   int num_squeezed_dims = 0;
     53   if (num_squeeze_dims == 0) {
     54     for (int idx = 0; idx < input_num_dims; ++idx) {
     55       if (input_dims->data[idx] == 1) {
     56         should_squeeze[idx] = true;
     57         ++num_squeezed_dims;
     58       }
     59     }
     60   } else {
     61     for (int idx = 0; idx < num_squeeze_dims; ++idx) {
     62       int current = squeeze_dims[idx] < 0 ? squeeze_dims[idx] + input_num_dims
     63                                           : squeeze_dims[idx];
     64       TF_LITE_ENSURE(context, current >= 0 && current < input_num_dims &&
     65                                   input_dims->data[current] == 1);
     66       if (!should_squeeze[current]) ++num_squeezed_dims;
     67       should_squeeze[current] = true;
     68     }
     69   }
     70   // Sets output dimensions.
     71   TfLiteIntArray* output_dims =
     72       TfLiteIntArrayCreate(input_num_dims - num_squeezed_dims);
     73   for (int in_idx = 0, out_idx = 0; in_idx < input_num_dims; ++in_idx) {
     74     if (!should_squeeze[in_idx]) {
     75       output_dims->data[out_idx++] = input_dims->data[in_idx];
     76     }
     77   }
     78   return context->ResizeTensor(context, op_context.output, output_dims);
     79 }
     80 
     81 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
     82   SqueezeContext op_context(context, node);
     83   TF_LITE_ENSURE_EQ(context, op_context.input->bytes, op_context.output->bytes);
     84   memcpy(op_context.output->data.raw, op_context.input->data.raw,
     85          op_context.input->bytes);
     86   return kTfLiteOk;
     87 }
     88 
     89 }  // namespace squeeze
     90 
     91 TfLiteRegistration* Register_SQUEEZE() {
     92   static TfLiteRegistration r = {nullptr, nullptr, squeeze::Prepare,
     93                                  squeeze::Eval};
     94   return &r;
     95 }
     96 
     97 }  // namespace builtin
     98 }  // namespace ops
     99 }  // namespace tflite
    100