/* 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 #include #include #include #include #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" namespace toco { namespace { template bool AreAllBufferElementsZero(const std::vector& buffer_data) { for (auto x : buffer_data) { if (x != 0) { return false; } } return true; } template void FillArrayWithZeros(Array* array) { CHECK(array->data_type == Type); std::vector>& data = array->GetMutableBuffer().data; data.resize(RequiredBufferSizeForShape(array->shape())); for (size_t i = 0; i < data.size(); i++) { data[i] = 0; } } } // namespace // Removes a multiplication by array of constant zeros by making the output // array an array of constant zeros and removing the input arrays if they are no // longer needed. ::tensorflow::Status ResolveMultiplyByZero::Run(Model* model, std::size_t op_index, bool* modified) { *modified = false; const auto mul_it = model->operators.begin() + op_index; auto* mul_op = mul_it->get(); if (mul_op->type != OperatorType::kMul) { return ::tensorflow::Status::OK(); } const auto& output_array_name = mul_op->outputs[0]; auto& output_array = model->GetArray(output_array_name); if (!IsDiscardableArray(*model, output_array_name)) { return ::tensorflow::Status::OK(); } if (output_array.data_type == ArrayDataType::kNone) { // Yield until the output type has been set by PropagateArrayDataTypes return ::tensorflow::Status::OK(); } // Yield if the output shape is not known yet. if (!output_array.has_shape()) { return ::tensorflow::Status::OK(); } // This transformation only handles the case where one operand is all 0's and // the other is non-constant. Other cases are handled by constant propagation // or the trivial binary removal pass. const bool is_input_constant[2] = { IsConstantParameterArray(*model, mul_op->inputs[0]), IsConstantParameterArray(*model, mul_op->inputs[1]), }; if (!is_input_constant[0] && !is_input_constant[1]) { // Neither input is constant, so nothing we can resolve here. return ::tensorflow::Status::OK(); } if (is_input_constant[0] && is_input_constant[1]) { // Both inputs are constants. That's a job for constants propagation, not // for us to handle here. return ::tensorflow::Status::OK(); } const int index_of_constant_input = is_input_constant[0] ? 0 : 1; const int index_of_variable_input = is_input_constant[0] ? 1 : 0; CHECK(is_input_constant[index_of_constant_input]); CHECK(!is_input_constant[index_of_variable_input]); const auto& constant_input_array = model->GetArray(mul_op->inputs[index_of_constant_input]); CHECK(constant_input_array.data_type == output_array.data_type); switch (output_array.data_type) { case ArrayDataType::kFloat: { const auto& constant_input_data = constant_input_array.GetBuffer().data; if (!AreAllBufferElementsZero>( constant_input_data)) { return ::tensorflow::Status::OK(); } FillArrayWithZeros(&output_array); } break; case ArrayDataType::kUint8: { const auto& constant_input_data = constant_input_array.GetBuffer().data; if (!AreAllBufferElementsZero>( constant_input_data)) { return ::tensorflow::Status::OK(); } FillArrayWithZeros(&output_array); } break; case ArrayDataType::kInt32: { const auto& constant_input_data = constant_input_array.GetBuffer().data; if (!AreAllBufferElementsZero>( constant_input_data)) { return ::tensorflow::Status::OK(); } FillArrayWithZeros(&output_array); } break; case ArrayDataType::kInt64: { const auto& constant_input_data = constant_input_array.GetBuffer().data; if (!AreAllBufferElementsZero>( constant_input_data)) { return ::tensorflow::Status::OK(); } FillArrayWithZeros(&output_array); } break; default: AddMessageF( "Cannot resolve multiply by 0 because of unsupported data type\n"); return ::tensorflow::Status::OK(); } // Erase input arrays to the multiply if no longer used DeleteArrayIfUsedOnce(mul_op->inputs[0], model); DeleteArrayIfUsedOnce(mul_op->inputs[1], model); // Erase the multiply operator. model->operators.erase(mul_it); *modified = true; return ::tensorflow::Status::OK(); } } // namespace toco