aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/resolve_space_to_batch_nd_attributes.cc
blob: 3804145c4f8cef7808306f565eba310aca5af811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
#include "tensorflow/contrib/lite/toco/model.h"
#include "tensorflow/contrib/lite/toco/tooling_util.h"
#include "tensorflow/core/platform/logging.h"

namespace toco {

::tensorflow::Status ResolveSpaceToBatchNDAttributes::Run(Model* model,
                                                          std::size_t op_index,
                                                          bool* modified) {
  *modified = false;
  const auto op_it = model->operators.begin() + op_index;
  if (op_it->get()->type != OperatorType::kSpaceToBatchND)
    return ::tensorflow::Status::OK();

  auto* op = static_cast<SpaceToBatchNDOperator*>(op_it->get());

  // The attributes are resolved only when the 3 attributes (block_shape,
  // before_paddings, after_paddings) are all constant.
  if (!op->block_shape.empty()) {
    return ::tensorflow::Status::OK();
  }

  const int block_shape_index = 1;
  const int paddings_index = 2;

  CHECK_EQ(op->inputs.size(), 3);
  if (!IsConstantParameterArray(*model, op->inputs[block_shape_index]) ||
      !IsConstantParameterArray(*model, op->inputs[paddings_index]))
    return ::tensorflow::Status::OK();

  // Handle paddings.
  const auto& paddings_array = model->GetArray(op->inputs[paddings_index]);
  if (!paddings_array.has_shape()) return ::tensorflow::Status::OK();
  const std::vector<int>& paddings_dims = paddings_array.shape().dims();
  if (paddings_dims.size() != 2) {
    // Code only handles padding of 2 dimensions. Perhaps another transformation
    // will delete this op.
    return ::tensorflow::Status::OK();
  }
  const std::vector<int>& paddings_buffer =
      paddings_array.GetBuffer<ArrayDataType::kInt32>().data;
  for (int i = 0; i < paddings_dims[0]; ++i) {
    op->before_paddings.push_back(paddings_buffer[i * 2]);
    op->after_paddings.push_back(paddings_buffer[i * 2 + 1]);
  }

  // Handle block_shape.
  const auto& block_shape_array =
      model->GetArray(op->inputs[block_shape_index]);
  if (!block_shape_array.has_shape()) return ::tensorflow::Status::OK();
  const std::vector<int>& block_shape_dims = block_shape_array.shape().dims();
  CHECK_EQ(block_shape_dims.size(), 1);
  const std::vector<int>& block_shape_buffer =
      block_shape_array.GetBuffer<ArrayDataType::kInt32>().data;
  for (int i = 0; i < block_shape_dims[0]; ++i) {
    op->block_shape.push_back(block_shape_buffer[i]);
  }

  *modified = true;
  return ::tensorflow::Status::OK();
}

}  // namespace toco