diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-03-29 11:02:56 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-03-29 11:05:50 -0700 |
commit | c194a0ea67a6fb61bd23b39fbb2a49b664e2dba1 (patch) | |
tree | 366b40536112bb9cd3daed22787dcbad2fdf6d86 | |
parent | 63dffd5a3bc4e94e74cb140cbf7a68e0e5644ad6 (diff) |
Automated g4 rollback of changelist 190808678
PiperOrigin-RevId: 190955400
6 files changed, 0 insertions, 278 deletions
diff --git a/tensorflow/contrib/lite/toco/BUILD b/tensorflow/contrib/lite/toco/BUILD index bba61627f9..d552de313c 100644 --- a/tensorflow/contrib/lite/toco/BUILD +++ b/tensorflow/contrib/lite/toco/BUILD @@ -280,7 +280,6 @@ cc_library( "graph_transformations/resolve_tensorflow_switch.cc", "graph_transformations/resolve_tensorflow_tile.cc", "graph_transformations/resolve_transpose_attributes.cc", - "graph_transformations/swap_elementwise_binary.cc", "graph_transformations/unfuse_activation_functions.cc", "graph_transformations/unpartition_embedding_lookup.cc", "graph_transformations/unroll_batch_matmul.cc", diff --git a/tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h b/tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h index 1291825c8e..640afc7c74 100644 --- a/tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h +++ b/tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h @@ -180,7 +180,6 @@ DECLARE_GRAPH_TRANSFORMATION(ResolveConstantStridedSlice) DECLARE_GRAPH_TRANSFORMATION(ResolveConstantFill) DECLARE_GRAPH_TRANSFORMATION(ResolveConstantGather) DECLARE_GRAPH_TRANSFORMATION(ResolveMultiplyByZero) -DECLARE_GRAPH_TRANSFORMATION(SwapElementwiseBinary) DECLARE_GRAPH_TRANSFORMATION(Dequantize) DECLARE_GRAPH_TRANSFORMATION(UnpartitionEmbeddingLookup) diff --git a/tensorflow/contrib/lite/toco/graph_transformations/swap_elementwise_binary.cc b/tensorflow/contrib/lite/toco/graph_transformations/swap_elementwise_binary.cc deleted file mode 100644 index ecbce58d16..0000000000 --- a/tensorflow/contrib/lite/toco/graph_transformations/swap_elementwise_binary.cc +++ /dev/null @@ -1,175 +0,0 @@ -/* Copyright 2018 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/runtime/types.h" -#include "tensorflow/contrib/lite/toco/tooling_util.h" -#include "tensorflow/core/platform/logging.h" - -namespace toco { - -namespace { - -bool ShapesAllowSwapping(const string& input_array_name, - const string& const_array_name, Model* model) { - const Array& input_array = model->GetOrCreateArray(input_array_name); - const Array& const_array = model->GetOrCreateArray(const_array_name); - // Wait until these shapes have been resolved. - if (!input_array.has_shape() || !const_array.has_shape()) { - return false; - } - - // Currently swapping is not handled for scalar const_array, though that could - // be done once there is a test model. - if (RequiredBufferSizeForShape(input_array.shape()) != - RequiredBufferSizeForShape(const_array.shape())) { - return false; - } - - return true; -} - -} // namespace - -// Swaps: -// Input -// \ -// (Reshape Op) Const -// \ / -// (Add/Sub/Mul/Div op) -// | -// Output -// -// To: -// -// Input Const -// \ / -// (Add/Sub/Mul/Div op) -// | -// (Reshape Op) -// | -// Output -// -// This can allow Add/Mul ops from batch normalization to be folded into an -// Input op from a FullyConnected layer. -bool SwapElementwiseBinary::Run(Model* model, std::size_t op_index) { - const auto element_wise_op_it = model->operators.begin() + op_index; - std::unique_ptr<Operator>& element_wise_op = *element_wise_op_it; - DCHECK(element_wise_op); - - switch (element_wise_op->type) { - case OperatorType::kAdd: - case OperatorType::kSub: - case OperatorType::kMul: - case OperatorType::kDiv: - break; - default: - return false; - } - - int reshape_input = -1; - Operator* op = GetOpWithOutput(*model, element_wise_op->inputs[0]); - if (!op) { - return false; - } - - if (op->type == OperatorType::kTensorFlowReshape) { - reshape_input = 0; - } else { - op = GetOpWithOutput(*model, element_wise_op->inputs[1]); - if (!op || op->type != OperatorType::kTensorFlowReshape) { - return false; - } - reshape_input = 1; - } - - int const_input = (reshape_input == 0) ? 1 : 0; - const string& const_input_array = element_wise_op->inputs[const_input]; - if (!IsConstantParameterArray(*model, const_input_array)) { - return false; - } - - // Do not fold division if denominator is not constant. - if (element_wise_op->type != OperatorType::kDiv && const_input != 1) { - return false; - } - - const auto reshape_it = - FindOpWithOutput(*model, element_wise_op->inputs[reshape_input]); - // Note: we take copies of the tensor names here, instead of const-refs as we - // may overwrite the original names. - const string reshape_input_name = (*reshape_it)->inputs[0]; - const string intermediate_name = (*reshape_it)->outputs[0]; - const string element_wise_output_name = element_wise_op->outputs[0]; - - // Check the reshape op input and const op have their shapes resolved. - if (!ShapesAllowSwapping(reshape_input_name, const_input_array, model)) { - return false; - } - - int count_ops_consuming_output = CountOpsWithInput(*model, intermediate_name); - DCHECK_GE(count_ops_consuming_output, 1); - if (count_ops_consuming_output > 1) { - AddMessageF( - "Not exchanging element-wise function with %s because it is " - "consumed by more than 1 other operator", - LogName(**reshape_it)); - return false; - } - - // If the element_wise_op was originally producing an output_array we can't - // swap as otherwise the output array would change. It'd be nice to still be - // able to swap but if code is relying on the fetch names instead of array - // indices this won't work. - for (int i = 0; i < model->flags.output_arrays_size(); ++i) { - if (model->flags.output_arrays(i) == element_wise_op->outputs[0]) { - AddMessageF( - "Not exchanging activation function with %s to preserve output array " - "name %s", - LogName(**reshape_it), element_wise_op->outputs[0]); - return false; - } - } - - // Rewire by changing inputs, including all consumers. - // TODO(b/76086261): Replace with new utility function. - Operator* consumer = GetFirstOpWithInput(*model, element_wise_output_name); - while (consumer) { - for (int i = 0; i < consumer->inputs.size(); ++i) { - if (consumer->inputs[i] == element_wise_output_name) { - consumer->inputs[i] = intermediate_name; - } - } - consumer = GetFirstOpWithInput(*model, element_wise_output_name); - } - element_wise_op->inputs[reshape_input] = reshape_input_name; - (*reshape_it)->inputs[0] = element_wise_output_name; - - // Clear shapes; this will allow shape propagation to fix the sizes for us. - model->GetOrCreateArray(element_wise_output_name).clear_shape(); - - // Finally, swap operators. Note that this only works when there are no other - // direct descendents of the reshape operator. - element_wise_op.swap(*reshape_it); - - return true; -} - -} // namespace toco diff --git a/tensorflow/contrib/lite/toco/graph_transformations/tests/BUILD b/tensorflow/contrib/lite/toco/graph_transformations/tests/BUILD index a2008ddbdb..8dcd4adc90 100644 --- a/tensorflow/contrib/lite/toco/graph_transformations/tests/BUILD +++ b/tensorflow/contrib/lite/toco/graph_transformations/tests/BUILD @@ -19,17 +19,6 @@ tf_cc_test( ) tf_cc_test( - name = "swap_elementwise_binary_test", - srcs = ["swap_elementwise_binary_test.cc"], - deps = [ - "//tensorflow/contrib/lite/toco:graph_transformations", - "//tensorflow/contrib/lite/toco:model", - "//tensorflow/contrib/lite/toco:tooling_util", - "@com_google_googletest//:gtest_main", - ], -) - -tf_cc_test( name = "lstm_utils_test", srcs = ["lstm_utils_test.cc"], deps = [ diff --git a/tensorflow/contrib/lite/toco/graph_transformations/tests/swap_elementwise_binary_test.cc b/tensorflow/contrib/lite/toco/graph_transformations/tests/swap_elementwise_binary_test.cc deleted file mode 100644 index c3778017f3..0000000000 --- a/tensorflow/contrib/lite/toco/graph_transformations/tests/swap_elementwise_binary_test.cc +++ /dev/null @@ -1,89 +0,0 @@ -/* Copyright 2018 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 <gtest/gtest.h> -#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 { - -int ShapeCount(const std::vector<int>& size) { - CHECK(size.size()); - int count = 1; - for (int dim : size) { - count *= dim; - } - return count; -} - -// Adds a new parameter array to the model. -void AddConstArray(const string& name, const float* data, - const std::vector<int>& size, Model* model) { - Array& array = model->GetOrCreateArray(name); - array.data_type = ArrayDataType::kFloat; - Shape* shape = array.mutable_shape(); - *(shape->mutable_dims()) = size; - - auto& buffer = array.GetMutableBuffer<toco::ArrayDataType::kFloat>(); - buffer.data.resize(ShapeCount(size)); - std::copy(data, data + ShapeCount(size), buffer.data.data()); -} - -} // namespace - -TEST(SwapElementwiseBinaryTest, SwapsReshape) { - Model model; - const float parameters[2][4] = {{0., 1., 2., 3.}, {10., 11., 12., 13.}}; - - AddConstArray("before_reshape", parameters[0], {2, 2}, &model); - AddConstArray("add_vector", parameters[1], {1, 4}, &model); - - auto reshape_op = absl::make_unique<TensorFlowReshapeOperator>(); - reshape_op->shape = {1, 4}; - reshape_op->inputs = {"before_reshape"}; - reshape_op->outputs = {"after_reshape"}; - Array& reshape_array = model.GetOrCreateArray("after_reshape"); - *(reshape_array.mutable_shape()) = {1, 4}; - - auto add_op = absl::make_unique<AddOperator>(); - add_op->inputs = {"after_reshape", "add_vector"}; - add_op->outputs = {"add"}; - Array& add_array = model.GetOrCreateArray("add"); - *(add_array.mutable_shape()) = {1, 4}; - - model.operators.push_back(std::move(reshape_op)); - model.operators.push_back(std::move(add_op)); - - auto transformation = absl::make_unique<toco::SwapElementwiseBinary>(); - ASSERT_TRUE(transformation->Run(&model, 1)); - - Operator* op = GetOpWithOutput(model, "add"); - ASSERT_NE(nullptr, op); - ASSERT_EQ(OperatorType::kAdd, op->type); - ASSERT_EQ(2, op->inputs.size()); - for (const string& input : op->inputs) { - EXPECT_TRUE(IsConstantParameterArray(model, input)) - << input << " is not const input"; - } -} - -} // namespace toco diff --git a/tensorflow/contrib/lite/toco/toco_tooling.cc b/tensorflow/contrib/lite/toco/toco_tooling.cc index 41ea1481bc..30dd6fab9e 100644 --- a/tensorflow/contrib/lite/toco/toco_tooling.cc +++ b/tensorflow/contrib/lite/toco/toco_tooling.cc @@ -90,7 +90,6 @@ void MakeGeneralGraphTransformationsSet( transformations->Add(new ResolveTensorFlowTile); transformations->Add(new ResolveTensorFlowConcat); transformations->Add(new ResolveMultiplyByZero); - transformations->Add(new SwapElementwiseBinary); transformations->Add(new IdentifyDilatedConv); transformations->Add(new IdentifyL2Normalization); transformations->Add(new IdentifyL2Pool); |