1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15#include <string.h> 16#include "tensorflow/contrib/lite/builtin_op_data.h" 17#include "tensorflow/contrib/lite/context.h" 18#include "tensorflow/contrib/lite/kernels/internal/tensor.h" 19#include "tensorflow/contrib/lite/kernels/kernel_util.h" 20#include "tensorflow/contrib/lite/kernels/op_macros.h" 21 22namespace tflite { 23namespace ops { 24namespace builtin { 25namespace reshape { 26 27constexpr int kInputTensor = 0; 28constexpr int kOutputTensor = 0; 29 30TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { 31 auto* params = reinterpret_cast<TfLiteReshapeParams*>(node->builtin_data); 32 33 // TODO(ahentz): we are often given a tensor with the shape but we only pay 34 // attention to what the shape specified in 'params'. 35 TF_LITE_ENSURE(context, NumInputs(node) == 1 || NumInputs(node) == 2); 36 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); 37 38 TfLiteTensor* input = GetInput(context, node, kInputTensor); 39 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 40 41 // Tensorflow's Reshape allows one of the shape components to have the 42 // special -1 value, meaning it will be calculated automatically based on the 43 // input. Here we calculate what that dimension should be so that the number 44 // of output elements in the same as the number of input elements. 45 int num_input_elements = 1; 46 for (int i = 0; i < NumDimensions(input); ++i) { 47 num_input_elements *= SizeOfDimension(input, i); 48 } 49 50 TfLiteIntArray* output_size = TfLiteIntArrayCreate(params->num_dimensions); 51 int num_output_elements = 1; 52 int strech_dim = -1; 53 for (int i = 0; i < params->num_dimensions; ++i) { 54 int value = params->shape[i]; 55 if (value == -1) { 56 TF_LITE_ENSURE_EQ(context, strech_dim, -1); 57 strech_dim = i; 58 } else { 59 num_output_elements *= value; 60 output_size->data[i] = value; 61 } 62 } 63 if (strech_dim != -1) { 64 output_size->data[strech_dim] = num_input_elements / num_output_elements; 65 num_output_elements *= output_size->data[strech_dim]; 66 } 67 68 TF_LITE_ENSURE_EQ(context, num_input_elements, num_output_elements); 69 return context->ResizeTensor(context, output, output_size); 70} 71 72TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 73 TfLiteTensor* input = GetInput(context, node, kInputTensor); 74 TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 75 76 memcpy(output->data.raw, input->data.raw, input->bytes); 77 78 return kTfLiteOk; 79} 80 81} // namespace reshape 82 83TfLiteRegistration* Register_RESHAPE() { 84 static TfLiteRegistration r = {nullptr, nullptr, reshape::Prepare, 85 reshape::Eval}; 86 return &r; 87} 88 89} // namespace builtin 90} // namespace ops 91} // namespace tflite 92