Home | History | Annotate | Download | only in graph_transformations
      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 <memory>
     16 #include <string>
     17 #include <unordered_map>
     18 #include <vector>
     19 
     20 #include "absl/strings/str_cat.h"
     21 #include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
     22 #include "tensorflow/contrib/lite/toco/model.h"
     23 #include "tensorflow/contrib/lite/toco/tooling_util.h"
     24 #include "tensorflow/core/platform/logging.h"
     25 
     26 namespace toco {
     27 
     28 bool ConvertExpandDimsToReshape::Run(Model* model, std::size_t op_index) {
     29   auto expand_it = model->operators.begin() + op_index;
     30   if (expand_it->get()->type != OperatorType::kExpandDims) {
     31     return false;
     32   }
     33   ExpandDimsOperator* expand_op =
     34       static_cast<ExpandDimsOperator*>(expand_it->get());
     35   CHECK_EQ(expand_op->inputs.size(), 2);
     36   CHECK_EQ(expand_op->outputs.size(), 1);
     37 
     38   const auto& input_array = model->GetArray(expand_op->inputs[0]);
     39   if (!input_array.has_shape()) {
     40     // Yield until input dims have been resolved.
     41     return false;
     42   }
     43   if (input_array.shape().dimensions_count() == 0) {
     44     // Input array cannot be 0-D.
     45     // (Unsure if this is TF behavior, but was required to get a test to pass.)
     46     return false;
     47   }
     48 
     49   const auto& axis_array = model->GetArray(expand_op->inputs[1]);
     50   if (!axis_array.has_shape()) {
     51     // Yield until input axis array shape has been resolved.
     52     return false;
     53   }
     54   CHECK_EQ(RequiredBufferSizeForShape(axis_array.shape()), 1);
     55   if (!axis_array.buffer) {
     56     // Yield until the input axis array is constant
     57     return false;
     58   }
     59   int axis = axis_array.GetBuffer<ArrayDataType::kInt32>().data[0];
     60   std::vector<int> reshape_dims(input_array.shape().dims());
     61   if (axis < 0) {
     62     axis = reshape_dims.size();
     63   }
     64   reshape_dims.insert(reshape_dims.begin() + axis, 1);
     65 
     66   // The input tensor has shape, and the axis input is constant. We can now
     67   // replace ExpandDims with a Reshape.
     68   auto* reshape_op = new TensorFlowReshapeOperator;
     69 
     70   // Copy inputs
     71   reshape_op->inputs.push_back(expand_op->inputs[0]);
     72   reshape_op->outputs = expand_op->outputs;
     73 
     74   // Create a new input array
     75   string axis_array_name = expand_op->inputs[1];
     76   string shape_array_name = toco::AvailableArrayName(*model, axis_array_name);
     77   Array& shape_array = model->GetOrCreateArray(shape_array_name);
     78   *(shape_array.mutable_shape()->mutable_dims()) = {
     79       1, static_cast<int>(reshape_dims.size())};
     80   reshape_op->inputs.push_back(shape_array_name);
     81   shape_array.data_type = ArrayDataType::kInt32;
     82   auto& shape_buffer = shape_array.GetMutableBuffer<ArrayDataType::kInt32>();
     83   shape_buffer.data = reshape_dims;
     84 
     85   // Delete axis array if unused
     86   if (IsDiscardableArray(*model, axis_array_name) &&
     87       CountOpsWithInput(*model, axis_array_name) == 1 &&
     88       !GetOpWithOutput(*model, axis_array_name)) {
     89     model->EraseArray(axis_array_name);
     90   }
     91 
     92   // Replace the operator in the graph.
     93   const auto reshape_it = model->operators.emplace(expand_it, reshape_op);
     94   expand_it = reshape_it + 1;
     95   CHECK_EQ(expand_it->get(), expand_op);
     96   model->operators.erase(expand_it);
     97 
     98   return true;
     99 }
    100 
    101 }  // namespace toco
    102