1 /* Copyright 2019 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 "tensorflow/core/lib/core/errors.h"
16 #include "tensorflow/core/platform/logging.h"
17 #include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
18 #include "tensorflow/lite/toco/model.h"
19 #include "tensorflow/lite/toco/tooling_util.h"
20
21 namespace toco {
22
23 // V3 is only different from V2 because it has an extra attribute (align).
24 // This attribute doesn't affect V1 so we don't have to keep track of it here.
Run(Model * model,std::size_t op_index,bool * modified)25 ::tensorflow::Status ConvertMatrixDiagV2OrV3ToV1::Run(Model* model,
26 std::size_t op_index,
27 bool* modified) {
28 *modified = false;
29 auto it = model->operators.begin() + op_index;
30 const auto* op = it->get();
31 if (op->type != OperatorType::kMatrixDiagV2 &&
32 op->type != OperatorType::kMatrixDiagV3) {
33 return ::tensorflow::OkStatus();
34 }
35
36 if (op->inputs.size() != 5) {
37 return tensorflow::errors::InvalidArgument(
38 "The input size of op %s should be 5", LogName(*op));
39 }
40
41 const auto& input_k = model->GetArray(op->inputs[1]);
42 const auto& input_num_rows = model->GetArray(op->inputs[2]);
43 const auto& input_num_cols = model->GetArray(op->inputs[3]);
44 const auto& input_padding_value = model->GetArray(op->inputs[4]);
45
46 if (!input_k.buffer || !input_num_rows.buffer || !input_num_cols.buffer ||
47 !input_padding_value.buffer) {
48 return ::tensorflow::OkStatus();
49 }
50
51 if (input_k.GetBuffer<ArrayDataType::kInt32>().data.size() != 1 ||
52 input_num_rows.GetBuffer<ArrayDataType::kInt32>().data.size() != 1 ||
53 input_num_cols.GetBuffer<ArrayDataType::kInt32>().data.size() != 1) {
54 return tensorflow::errors::InvalidArgument(
55 "Array for argument k / num_rows / num_cols of op ", LogName(*op),
56 " should contains exact one element");
57 }
58
59 int k = input_k.GetBuffer<ArrayDataType::kInt32>().data[0];
60 int num_rows = input_num_rows.GetBuffer<ArrayDataType::kInt32>().data[0];
61 int num_cols = input_num_cols.GetBuffer<ArrayDataType::kInt32>().data[0];
62 const auto& padding_value_vector =
63 input_padding_value.GetBuffer<ArrayDataType::kUint8>().data;
64
65 if (k != 0) {
66 return tensorflow::errors::InvalidArgument(
67 "parameter k of op ", LogName(*op),
68 " is expected to be 0, other values are not supported currently");
69 }
70
71 if (num_rows != -1) {
72 return tensorflow::errors::InvalidArgument(
73 "parameter num_rows of op ", LogName(*op),
74 " is expected to be -1, other values are not supported currently");
75 }
76
77 if (num_cols != -1) {
78 return tensorflow::errors::InvalidArgument(
79 "parameter num_cols of op ", LogName(*op),
80 " is expected to be -1, other values are not supported currently");
81 }
82 for (auto byte : padding_value_vector) {
83 if (byte != 0) {
84 return tensorflow::errors::InvalidArgument(
85 "parameter padding_value of op ", LogName(*op),
86 " is expected to be 0, other values are not supported currently");
87 }
88 }
89
90 auto* matrix_diag_op = new MatrixDiagOperator;
91 matrix_diag_op->inputs.push_back(op->inputs[0]);
92 matrix_diag_op->outputs.push_back(op->outputs[0]);
93
94 AddMessageF("Replacing %s with %s", LogName(*op), LogName(*matrix_diag_op));
95
96 // Replace the operator in the graph.
97 model->operators.emplace(it, matrix_diag_op);
98 DeleteOpAndArrays(model, op);
99
100 *modified = true;
101 return ::tensorflow::OkStatus();
102 }
103
104 } // namespace toco
105