aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/resolve_constant_range.cc
blob: 069d4dafaabd41e79adc27083f8c14e164e22ff3 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* 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 "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 ResolveConstantRange::Run(Model* model,
                                               std::size_t op_index,
                                               bool* modified) {
  *modified = false;
  const auto it = model->operators.begin() + op_index;
  auto* base_op = it->get();
  if (base_op->type != OperatorType::kRange) {
    return ::tensorflow::Status::OK();
  }
  auto* op = static_cast<RangeOperator*>(base_op);

  CHECK_EQ(op->inputs.size(), 3);
  const auto& start_array = model->GetArray(op->inputs[0]);
  if (!start_array.has_shape()) {
    // Yield until all input dims have been resolved.
    return ::tensorflow::Status::OK();
  }
  const auto& limit_array = model->GetArray(op->inputs[1]);
  if (!limit_array.has_shape()) {
    // Yield until all input dims have been resolved.
    return ::tensorflow::Status::OK();
  }
  const auto& delta_array = model->GetArray(op->inputs[2]);
  if (!delta_array.has_shape()) {
    // Yield until all input dims have been resolved.
    return ::tensorflow::Status::OK();
  }

  for (const auto& input : op->inputs) {
    if (!IsConstantParameterArray(*model, input)) {
      // yield if any input is mutable
      return ::tensorflow::Status::OK();
    }
  }

  CHECK_EQ(op->outputs.size(), 1);
  auto& output_array = model->GetArray(op->outputs[0]);
  if (output_array.data_type == ArrayDataType::kNone) {
    // Yield until the output type has been set by PropagateArrayDataTypes
    return ::tensorflow::Status::OK();
  }

  CHECK_EQ(RequiredBufferSizeForShape(start_array.shape()), 1)
      << "Range op inputs must be scalar.";
  CHECK_EQ(RequiredBufferSizeForShape(limit_array.shape()), 1)
      << "Range op inputs must be scalar.";
  CHECK_EQ(RequiredBufferSizeForShape(delta_array.shape()), 1)
      << "Range op inputs must be scalar.";

  CHECK(start_array.data_type == ArrayDataType::kInt32)
      << "Range op inputs must be int32.";
  CHECK(limit_array.data_type == ArrayDataType::kInt32)
      << "Range op inputs must be int32.";
  CHECK(delta_array.data_type == ArrayDataType::kInt32)
      << "Range op inputs must be int32.";

  // Compute buffer contents
  int start = start_array.GetBuffer<ArrayDataType::kInt32>().data[0];
  int limit = limit_array.GetBuffer<ArrayDataType::kInt32>().data[0];
  int delta = delta_array.GetBuffer<ArrayDataType::kInt32>().data[0];
  auto& buffer = output_array.GetMutableBuffer<ArrayDataType::kInt32>();
  buffer.data.clear();
  for (int32 val = start; val < limit; val += delta) {
    buffer.data.push_back(val);
  }
  CHECK_EQ(floor((limit - start) / delta), buffer.data.size());
  CHECK_EQ(buffer.data.size(), output_array.shape().dims()[0]);

  // Delete the input array if no longer used
  if (IsDiscardableArray(*model, op->inputs[0]) &&
      CountOpsWithInput(*model, op->inputs[0]) == 1) {
    model->EraseArray(op->inputs[0]);
  }
  if (IsDiscardableArray(*model, op->inputs[1]) &&
      CountOpsWithInput(*model, op->inputs[1]) == 1) {
    model->EraseArray(op->inputs[1]);
  }
  if (IsDiscardableArray(*model, op->inputs[2]) &&
      CountOpsWithInput(*model, op->inputs[2]) == 1) {
    model->EraseArray(op->inputs[2]);
  }

  // Delete the operator
  model->operators.erase(it);

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

}  // namespace toco