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 "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
     21 #include "tensorflow/contrib/lite/toco/model.h"
     22 #include "tensorflow/contrib/lite/toco/tooling_util.h"
     23 #include "tensorflow/core/platform/logging.h"
     24 
     25 namespace toco {
     26 
     27 bool ResolveBatchToSpaceNDAttributes::Run(Model* model, std::size_t op_index) {
     28   const auto op_it = model->operators.begin() + op_index;
     29   if (op_it->get()->type != OperatorType::kBatchToSpaceND) return false;
     30 
     31   auto* op = static_cast<BatchToSpaceNDOperator*>(op_it->get());
     32 
     33   // The attributes are resolved only when the 3 attributes (block_shape,
     34   // before_crops, after_crops) are all constant.
     35   if (!op->block_shape.empty()) {
     36     return false;
     37   }
     38 
     39   CHECK_EQ(op->inputs.size(), 3);
     40   if (!IsConstantParameterArray(*model, op->inputs[1]) ||
     41       !IsConstantParameterArray(*model, op->inputs[2]))
     42     return false;
     43 
     44   // Handle crops
     45   const auto& crops_array = model->GetArray(op->inputs[2]);
     46   if (!crops_array.has_shape()) return false;
     47   const std::vector<int>& crops_dims = crops_array.shape().dims();
     48   if (crops_dims.size() != 2) {
     49     // Code only handles crops of 2 dimensions. Perhaps another transformation
     50     // will delete this op.
     51     return false;
     52   }
     53   std::vector<int> crops_buffer =
     54       crops_array.GetBuffer<ArrayDataType::kInt32>().data;
     55   for (int i = 0; i < crops_dims[0]; ++i) {
     56     op->before_crops.push_back(crops_buffer[i * 2]);
     57     op->after_crops.push_back(crops_buffer[i * 2 + 1]);
     58   }
     59 
     60   // Handle block_shape
     61   const auto& block_shape_array = model->GetArray(op->inputs[1]);
     62   if (!block_shape_array.has_shape()) return false;
     63   const std::vector<int>& block_shape_dims = block_shape_array.shape().dims();
     64   CHECK_EQ(block_shape_dims.size(), 1);
     65   std::vector<int> block_shape_buffer =
     66       block_shape_array.GetBuffer<ArrayDataType::kInt32>().data;
     67   for (int i = 0; i < block_shape_dims[0]; ++i) {
     68     op->block_shape.push_back(block_shape_buffer[i]);
     69   }
     70 
     71   return true;
     72 }
     73 
     74 }  // namespace toco
     75